向应用程序发送信号(Windows)
Sending signals to applications (Windows)
我正在疯狂地思考如何用C++在Windows上向外部.exe发送信号。一个家伙"user1764961",建议使用Mutex,但我真的不明白它是如何工作的!我也在读这一页:http://www.tutorialspoint.com/cplusplus/cpp_signal_handling.htm但我不明白。。。
例如:我想向"无窗口".exe发送关闭信号如果需要详细信息,我知道关于.exe的一切。
试试这个。既不优雅也不安全(非常非常不安全(,而是解决问题的最简单方法。
在要关闭的应用程序中执行以下操作:
DWORD dwProcessID;
HANDLE hProcess, hMutex;
hProcess = GetCurrentProcess();
DuplicateHandle(hProcess, hProcess, hProcess, &hProcess, NULL, TRUE, DUPLICATE_SAME_ACCESS);
// write retrieved handle somewhere in file. let it be "C:sample.txt"
// ...
hMutex = CreateMutex(NULL, TRUE, L"Look at me! I'm a scarry MUTEX");
//... Your code
// go close your video streams or do whatever you want
// ...
ReleaseMutex(hMutex);
在您的应用程序中:
// so now the distant proc works and you now it
// it is time to terminate it.
HANDLE hMutex, hProcess;
// read hProcess from "C:sample.txt"
hMutex = OpenMutex(SYNCHRONIZE, FALSE, L"Look at me! I'm scary MUTEX");
WaitForSingleObject(hMutex, INFINITE); //if your "video app" will not release the mutex you will wait forever.
TerminateProcess(hProcess, 0); //that's what you need
Mutex是一个核心对象。你可以把它想象成一个由车主操纵的红绿灯。如果行人想活着上马路,就应该遵守规定。
在给定的例子中有两个开放的问题。
首先,您不应该在文件中写入进程句柄。那么如何处理流程呢?看这里。
其次,正如我之前所说,除非你是上帝,否则你不能永远等待。请确保您的进程将释放互斥对象或设置WaitForSingleObject(hMutex, TIME_INTERVAL)
。TIME_INTERVAL是DWORD。选择它。
我希望我的回答能对你有所帮助。
相关文章:
- Qt VTK交互风格的信号到小部件
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux for Windows上编译C++代码时出错
- C++ - 程序接收信号SIGSEGV,分段错误.In msvcrt!memcpy () (C:\Windows\S
- 我应该使用什么信号来终止/终止Windows上的应用程序
- QClipboard的信号选择已更改在Windows 7中不起作用
- 如何使用Windows API直接将进程"assign"到信号量?
- 子进程 c++ 的 Windows 控制台信号处理
- 注册以在 MFC 中发出 Win32 事件信号时接收 Windows 消息
- 信号插槽在windows下连接,在linux下连接NOT
- 在WINDOWS中从子进程向父进程发送信号
- Windows高速模式(C++):等待信号量(WaitForSingleObjectEx):失败,访问被拒绝[部分解决]
- Windows/C++:如何从信号处理程序中获取有用的堆栈跟踪
- 如何强制Windows向所有应用程序发送'LOW_MEMORY'信号?
- Windows信号量和QSystemSemaphore
- 如何在windows平台上使用c++信号
- Windows上的快速计数信号量
- 向应用程序发送信号(Windows)
- 在出现term信号时,Windows处理退出代码
- Windows 中的 Control+Z 表示输入 c++ 结束的信号