How to disable the close button of console using C

The code for making the close button disabled is given below. It disables the close button and hence the user cannot close the console. But one can close console using Task Manager.

#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <windows.h>
int main(int argc, _TCHAR* argv[])
{
    HWND h;
    HMENU sm;
    int i, j, c;
    LPTSTR buf;
    // get the handle to the console
    h = GetConsoleWindow();
    // get handle to the System Menu
    sm = GetSystemMenu(h, 0);
    // how many items are there?
    c = GetMenuItemCount(sm);
    j = -1;
    buf = (TCHAR*) malloc (256 *sizeof(TCHAR));
    for (i=0; i<c; i++) {
        // find the one we want
        GetMenuString(sm, i, buf, 255, MF_BYPOSITION);
        if (!strcmp(buf, "&Close")) {
            j = i;
            break;
        }
    }
    // if found, remove that menu item
    if (j >= 0)
        RemoveMenu(sm, j, MF_BYPOSITION);
    return 0;
}
SHARE How to disable the close button of console using C

You may also like...

Leave a Reply

Your email address will not be published.

Share