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

2

14

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.

Chrysarobin answered 14/11, 2012 at 1:11 Comment(2)
Depending on what that's for, you may find the Console project useful.Adjutant
It's interesting because I was going to say you could grab the WNDCLASSEX associated with it and make your own window based off of that one, but it turns out grabbing the WNDCLASSEX is a bit hard. GetClassInfoEx needs a module handle, and getting that handle is annoying. Even CreateToolhelp32Snapshot clearly states that you cannot use it with csrss.exe.Zeiler
C
16

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:

Clymer answered 18/11, 2012 at 9:27 Comment(3)
I'm not really looking for a "it can't be done" answer, but you did give me some interesting links, so I upvoted you. I've heard of people being able to do this kind of stuff, and I'm interested in how they did it. I did a quick Google search, and I found this: codeforge.com/read/115608/CsrssHook.c__html - It seems that this is a sample code used to hook into CSRSS. I guess that would be some kind of proof that it can be done..?Chrysarobin
The sample you referred to implements code injection into target process, so that you could provide your own WndProc within that process... This might work out if you have sufficient permissions to hook system process, however. Don't be surprised getting ERROR_ACCESS_DENIED for OpenProcess(PROCESS_ALL_ACCESS, ....Clymer
In Windows 7+ the console window is hosted by conhost.exe, which runs in the security context of the client, i.e. in this case you have PROCESS_ALL_ACCESS.Cosecant
I
1

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.

Italicize answered 14/11, 2012 at 1:34 Comment(1)
His own console window belongs to another process,Lavolta

© 2022 - 2024 — McMap. All rights reserved.