[英]How to install a hook on a process created with CreateProcess?
這是我嘗試的:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
cout << "Starting Notepad++..." << endl;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
// set the size of the structures
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
ZeroMemory(&processInformation, sizeof(processInformation));
char commandLine[] = "C:\\Program Files\\Notepad++\\Notepad++.exe";
// start the program up
BOOL res = CreateProcess(NULL, // the path
commandLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startupInfo, // Pointer to STARTUPINFO structure
&processInformation // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
);
if (res) {
if (!(mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, processInformation.dwThreadId))) {
cout << "Failed to install mouse hook :" << endl << getLastErrorAsString() << endl;
}
WaitForSingleObject( processInformation.hProcess, INFINITE );
CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
} else {
cout << "Failed to start Notepad++" << endl;
}
return 0;
}
它可以成功啟動Notepad ++,但無法安裝該掛鈎,並且GetLastError返回以下錯誤: The parameter is incorrect.
。 我不知道哪個參數不正確。 但是,當我關閉Notepad ++時,程序正常完成。
由於我是在主程序中啟動該過程的,並且該鈎子回調也在主程序中,因此我應該能夠安裝鈎子而無需進行任何dll注入。
我已經好幾年沒接觸過c ++了,而且我從未涉足系統開發,所以我這樣做的方式可能不對,所以您可以向我解釋我的錯誤在哪里嗎?
編輯:你們都告訴我,我需要注入一個dll來掛接特定進程,但這是從SetWindowsHookEx
的Windows文檔中獲取的有關hMod參數(第3個參數)的信息:
DLL的句柄,其中包含由lpfn參數指向的掛鈎過程。 如果dwThreadId參數指定由當前進程創建的線程,並且掛鈎過程在與當前進程關聯的代碼內,則hMod參數必須設置為NULL。
我的線程是由當前進程創建的,而我的鈎子過程位於當前進程的代碼內,那么為什么當我使用非低級鈎子(WH_MOUSE)時,它不起作用?
在評估輸入的目標之前,將執行低級掛鈎。 這就是為什么低級掛鈎需要全局的原因,如SetWindowsHookEx文檔中所述。 您不能為dwThreadId參數傳遞非零值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.