Tried searching but everything seems to be for VB C++. Is this even possible in dev c++ if so please tell me how to do it

Recommended Answers

All 7 Replies

Tried searching but everything seems to be for VB C++. Is this even possible in dev c++ if so please tell me how to do it

Dev-C++ has no its own console window, it's Windows OS console window of your application started from the Dev-C++ IDE.
VB C++ - what is it?!

I mean visual c++ also no thats no go for doing it in dev c++?

After a little testing I don't think its possible to prevent someone from closing the console window. I tried the code below with vc++ 2008 Express, but it doesn't work because SetWindowLong() returns 0 and the windows error message is "Access is denied". I assume its a permission thing.

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
WNDPROC dwPrevWinProc = 0;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
{
    if( message == WM_CLOSE)
        return 0;
    return CallWindowProc(dwPrevWinProc, hwnd, message, wParam, lParam);
}

int main()
{
    HWND hWnd = GetConsoleWindow();
    SetLastError(0);
    dwPrevWinProc = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProc);
    if( dwPrevWinProc == 0)
    {
        DWORD dwError = GetLastError();
        char buf[255];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwError, 0, buf, sizeof(buf), 0);
        cout << buf << "\n";
    }
    else
    {
        string str;
        cout << "Enter something\n";
        cin >> str;
        SetWindowLong(hWnd, GWL_WNDPROC, (LONG)dwPrevWinProc);
    }
}

This deactivates the close option. Remember that you'll have to catch ctrl-C too.

#define _WIN32_WINNT 0x0500
#include <windows.h>
int main() {
  HWND hwnd = GetConsoleWindow();
  HMENU hmenu = GetSystemMenu (hwnd, FALSE);
  HINSTANCE hinstance =
    (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE);
  while (DeleteMenu (hmenu, 0, MF_BYPOSITION))
      ;
  system ("pause");
}
commented: Very Good :) :) +36

That certainly works but disables the entire console menu, not just the close menu item.

I leave its improvement as an exercise to the OP. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.