简体   繁体   中英

C++: How to set a new wndProc for a console application?

If I have a console application with a handle to it set up like so;

HWND hWnd = GetConsoleWindow();

Then how do I set up a new wndProc for the window?
I tried using

SetWindowLong(hWnd, GWL_WNDPROC, (LONG)conProc);

With conProc being defined as

LRESULT CALLBACK conProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_NCHITTEST:
            return HTCAPTION;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam );
}

But it doesn't work and says "Error code: 5 - Access is denied" on GetLastError()

I understand that it's pretty difficult to modify the console application like this, since it's a csrss.exe application and all, but I'd still like to try.. Thanks.

While the impression is that console window belongs to your process (like other window), it is in fact hosted by CSRSS system process and its WndProc is there. This makes you unable to subclass the window and provide your own WndProc living in your process.

Some related reading:

First of all SetWindowLong is superseded by SetWindowLongPtr, you should use that function.

Are you trying to change the WNDPROC of your own console window or another process?

From the MSDN docs :

GWL_WNDPROC -4 Sets a new address for the window procedure. You cannot change this attribute if the window does not belong to the same process as the calling thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM