简体   繁体   English

C ++:如何为控制台应用程序设置新的wndProc?

[英]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? 那我该如何为窗口设置一个新的wndProc呢?
I tried using 我试过用

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

With conProc being defined as 将conProc定义为

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() 但它不起作用,并在GetLastError()上说“错误代码:5 - 访问被拒绝”

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. 我知道修改这样的控制台应用程序非常困难,因为它是一个csrss.exe应用程序,但我仍然想尝试..谢谢。

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. 虽然印象是控制台窗口属于您的过程(与其他窗口一样),但它实际上由CSRSS系统进程托管,其WndProc就在那里。 This makes you unable to subclass the window and provide your own WndProc living in your process. 这使您无法对窗口进行子类化,并在您的过程中提供自己的WndProc。

Some related reading: 一些相关阅读:

First of all SetWindowLong is superseded by SetWindowLongPtr, you should use that function. 首先SetWindowLong被SetWindowLongPtr取代,你应该使用该函数。

Are you trying to change the WNDPROC of your own console window or another process? 您是否尝试更改自己的控制台窗口或其他进程的WNDPROC?

From the MSDN docs : 来自MSDN文档:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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