gpt4 book ai didi

c++ - 向其他进程发送消息

转载 作者:可可西里 更新时间:2023-11-01 11:48:39 27 4
gpt4 key购买 nike

for (int i = 0; i < n; i++)
{
const char* cstr = strings[i].c_str();
swprintf_s(fullCommandLine, L"\"%s\" \"%s\" %S", pathToModule, pathToFile, cstr);
if(CreateProcess(NULL,
(LPWSTR)fullCommandLine,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
cout << "succes";
}
else cout << "fail";
}

我正在创建 n 个进程来像这样在给定的文件中查找字符串,并且在我的模块中(wchich 在文件中查找给定的字符串)我想向其他 n-1 个进程发送消息以退出

while (file >> readout)
{
if (readout == search)
{
cout << "I found string";
SendMessage(/*what should be here*/);
}
}

我从哪里可以获得那些其他进程的句柄?

最佳答案

请看我的PostThreadMessage to Console Application .

我创建它是因为肯定可以向控制台程序发送消息,我们只需要创建一个消息循环,就像可以从控制台程序显示一个窗口一样。

注意 PostThreadMessage 需要一个线程 id,而不是进程 id。每个进程也有一个线程 ID,进程的线程 ID 在 CreateProcess 的 PROCESS_INFORMATION 中。

以下是一个更大但更易于使用的示例,用于演示 PostThreadMessage 在控制台程序中的工作。如果没有参数,该程序将调用自身(传递其线程 ID),然后它将等待新进程发送消息。如果有一个参数,那么它将假定该参数是一个线程 ID,并向该线程发送一条消息,然后是一个 WM_QUIT。

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szCmdline[300];
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
BOOL bSuccess = FALSE;

ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = NULL;
siStartInfo.hStdOutput = NULL;
siStartInfo.hStdInput = NULL;

DWORD dwThread;
MSG Msg;
TCHAR ThreadIdBuffer[40];

// if no argument then execute ourself then wait for a message from that thread
if (argc == 1) {
_itot_s(GetCurrentThreadId(), ThreadIdBuffer, 40, 10);
szCmdline[0] = '"';
szCmdline[1] = 0;
_tcscat_s(szCmdline, 300, argv[0]); // ourself
int n = _tcslen(szCmdline);
szCmdline[n++] = '"';
szCmdline[n++] = ' ';
szCmdline[n++] = 0;
_tcscat_s(szCmdline, 300, ThreadIdBuffer); // our thread id
bSuccess = CreateProcess(argv[0], // execute ourself
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
if (!bSuccess) {
std::cout << "Process not started\n";
return 0;
}
std::cout << "Waiting\n";
// Now wait for the other process to send us a message
while (GetMessage(&Msg, NULL, 0, WM_USER)) {
if (Msg.message == WM_COMMAND)
std::cout << "WM_COMMAND\n";
else
std::cout << "Message: " << Msg.message << '\n';
}
std::cout << "End of message loop\n";
return 0;
}

// if there is an argument then assume it is a threadid of another one of us
std::cout << "Press Enter to send the message\n";
if (std::wcin.get() != '\n')
return 0;
dwThread = _wtoi(argv[1]);
if (!PostThreadMessage(dwThread, WM_COMMAND, (WPARAM)0, (LPARAM)0))
std::cout << GetLastError() << " PostThreadMessage error\n";
if (!PostThreadMessage(dwThread, WM_QUIT, (WPARAM)0, (LPARAM)0))
std::cout << GetLastError() << " PostThreadMessage error\n";
return 0;
}

关于c++ - 向其他进程发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677559/

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