13

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.

  • Depending on what that's *for*, you may find the [Console project](http://sourceforge.net/projects/console/) useful. – Cheers and hth. - Alf Nov 14 '12 at 02:13
  • 2
    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. – chris Nov 14 '12 at 02:21

2 Answers2

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:

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • 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: http://www.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..? –  Nov 18 '12 at 09:38
  • 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, ...`. – Roman R. Nov 18 '12 at 09:48
  • 1
    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`. – Eryk Sun Jun 05 '15 at 12:09
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.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66