我试图子类化另一个窗口(在另一个进程中),所以我注入了一个调用 SetWindowLongPtr 的 dll,但它失败了,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);
}
}
感谢您的阅读!