将消息发送到其他进程

时间:2016-04-17 14:05:37

标签: c++ windows messaging

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*/);
    }
}

从哪里可以获得句柄到其他进程?

1 个答案:

答案 0 :(得分:2)

请参阅我的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;
}