C++ 向其他进程发送消息 for(int i=0;i

C++ 向其他进程发送消息 for(int i=0;i,c++,windows,messaging,C++,Windows,Messaging,我创建它是因为它确实可以向控制台程序发送消息,我们只需要创建一个消息循环,就像可以从控制台程序显示一个窗口一样 请注意,PostThreadMessage需要一个线程id,而不是进程id。每个进程都有一个线程id,并且进程的线程id位于CreateProcess的进程_信息中 下面是一个更大的示例,但更易于用于演示PostThreadMessage在控制台程序中工作。此程序将调用自身(传递其线程id)如果没有参数,则它将等待新进程发送消息。如果有参数,则它将假定参数是线程id,并向该线程发送消息

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

请注意,PostThreadMessage需要一个线程id,而不是进程id。每个进程都有一个线程id,并且进程的线程id位于CreateProcess的进程_信息中

下面是一个更大的示例,但更易于用于演示PostThreadMessage在控制台程序中工作。此程序将调用自身(传递其线程id)如果没有参数,则它将等待新进程发送消息。如果有参数,则它将假定参数是线程id,并向该线程发送消息,然后执行WM_退出

while (file >> readout)
{
    if (readout == search)
    {
        cout << "I found string";
        SendMessage(/*what should be here*/);
    }
}
#包括“stdafx.h”
int _tmain(int argc,_TCHAR*argv[]
{
TCHAR szCmdline[300];
处理信息piProcInfo;
STARTUPINFO-siStartInfo;
BOOL bsucces=假;
零内存(&piProcInfo,sizeof(进程信息));
ZeroMemory(&siStartInfo,sizeof(STARTUPINFO));
siStartInfo.cb=sizeof(STARTUPINFO);
siStartInfo.hStdError=NULL;
siStartInfo.hStdOutput=NULL;
siStartInfo.hStdInput=NULL;
德沃德·德沃德;
味精;
TCHAR ThreadIdBuffer[40];
//如果没有参数,那么执行我们自己,然后等待来自该线程的消息
如果(argc==1){
_itot_s(GetCurrentThreadId(),ThreadIdBuffer,40,10);
szCmdline[0]=“”;
szCmdline[1]=0;
_tcscat_s(szCmdline,300,argv[0]);//我们自己
int n=_tcslen(szCmdline);
szCmdline[n++]='”;
szCmdline[n++]='';
szCmdline[n++]=0;
_tcscat_s(szCmdline,300,ThreadIdBuffer);//我们的线程id
bSuccess=CreateProcess(argv[0],//自行执行
szCmdline,//命令行
NULL,//进程安全属性
NULL,//主线程安全属性
TRUE,//句柄被继承
0,//创建标志
NULL,//使用父环境
NULL,//使用父级的当前目录
&siStartInfo,//STARTUPINFO指针
&piProcInfo);//接收进程信息
如果(!b成功){

std::这不应该编译,你没有将正确的参数传递给。至于你的问题,你难道不想知道最后一个参数是用来做什么的吗?它在结构中设置了什么信息?这是一个很好的开始。@JoachimPileborg我只是没有展示完整的代码,我将阅读关于这个
过程信息的内容
谢谢。另一个过程是例如,如果另一个程序是控制台程序,则使用带有消息循环的窗口程序?
SendMessage
将不起作用。您还可以查看
TerminateProcess
@Barmak Shemirani,是的,SendMessage可能不适用于控制台程序,但PostThreadMessage会。SendMessage需要窗口句柄,但PostThreadMessage需要一个线程ID。它可以在控制台程序中创建一个窗口,这样就可以在控制台程序中创建一个隐藏窗口。@ IcTestTaby,你认为这在某种程度上是相关的吗?我不认为它是相关的。既然你建议使用<代码> PrxTraceMeule>代码,我就把一条链接贴到一篇文章中,解释为什么和何时T。他的API调用不再可靠。我认为这在某种程度上是相关的。@ iTestTable,文章说:“用一个也显示UI的线程”。这是关于UI消息的警告,与此无关。对于窗口,没有必要使用PostThreadMessage,因为会有一个窗口句柄。@alias65536它是固定的。我不知道发生的方式和时间,但域名在URL中出现了两次。
while (file >> readout)
{
    if (readout == search)
    {
        cout << "I found string";
        SendMessage(/*what should be here*/);
    }
}
#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;
}