如何在使用CreateProcess创建的进程上安装挂钩
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参数(第三个参数(:
DLL的句柄,该句柄包含lpfn参数。如果dwThreadId参数指定由当前进程创建的线程并且如果钩子过程在与当前过程。
我的线程是由当前进程创建的,而我的钩子过程在我当前进程的代码中,那么为什么当我使用非低级钩子(WH_MOUSE(时它不起作用呢?
在评估输入的目的地之前,执行低级挂钩。正如SetWindowsHookEx文档中所解释的那样,这就是为什么低级别钩子需要是全局的原因。不能为dwThreadId参数传递非零值。
相关文章:
- boost::进程间消息队列引发错误
- 在Ubuntu 16.04上安装Cilk时出现问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 是否可以通过C++扩展强制多个python进程共享同一内存
- IPC使用多个管道和分支进程来运行Python程序
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- C++:如何在CLion IDE中安装Boost
- 异常属于C++中的线程还是进程
- WMI检测进程创建事件-c++
- c++多进程编写一个唯一的文件
- 如何在C++中将函数发送到另一个进程
- 安装opencv失败-粘贴CMakeError.log的内容
- 在cygwin中测试新的boost安装时出现cpp错误
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 有人安装"IITB Simplecpp in mac"吗?
- 终止 QProcess 不会终止子进程
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 如何在使用CreateProcess创建的进程上安装挂钩
- Cygwin安装说明:将安装移交给提升的子进程
- 如何挂接到一些Windows安装程序进程