gpt4 book ai didi

c - SetWindowLongPtr 似乎不起作用

转载 作者:行者123 更新时间:2023-11-30 14:22:06 28 4
gpt4 key购买 nike

我尝试子类化另一个窗口(在另一个进程中),因此我注入(inject)了一个 dll,它调用 SetWindowLongPtr,但它失败并且 GetLastError 返回 5。

BOOL APIENTRY DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
{
HWND hwnd = GetHwndProc();

if (!(orgWndProc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)SubclassProc)))
{
char buf[40];
sprintf(buf, "Error code: %d", GetLastError());
MessageBox(hwnd, buf, "Error", MB_OK);
}
break;
}
}
return TRUE;
}

编辑:它绝对是正确的 PID。

编辑2:我得到了错误的HWND,但现在已修复(也编辑了代码)我不再收到错误 5(来自 GetLastError)

    HWND GetHwndProc()
{
HWND hwnd = GetTopWindow(NULL);
DWORD currentPID = GetCurrentProcessId();
do
{
char title[256];
if ((GetWindowText(hwnd, title, 256) > 0) && (IsWindowVisible(hwnd)))
{
DWORD procId;
GetWindowThreadProcessId(hwnd, &procId);

if (procId == currentPID)
{
MessageBox(hwnd, title, "", MB_OK);
return hwnd;
}
}

hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
} while (hwnd);
}

WNDPROC orgWndProc;
LRESULT APIENTRY SubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_LBUTTONDOWN:
MessageBox(0, "Subclass", "", 0);
return TRUE;

default:
return CallWindowProc(orgWndProc, hwnd, msg, wParam, lParam);
}
}

感谢您的阅读!

最佳答案

您需要从创建窗口的线程调用SetWindowSubclass,与之关联的消息队列在该线程中运行。来自SetWindowSubclass reference :

Warning You cannot use the subclassing helper functions to subclass a window across threads.

反过来,SetWindowLongPtr 必须从创建窗口的进程中调用。来自SetWindowLongPtr reference :

Windows XP/2000: The SetWindowLongPtr function fails if the window specified by the hWnd parameter does not belong to the same process as the calling thread.

还有用户界面权限隔离,它进一步限制访问。

关于c - SetWindowLongPtr 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110720/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com