Search code examples
c++windowsmessaging

Sending Message to other process


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";
}

I'm creating n procesess to find string in given file like this, and In my module(wchich looks for given string in file) I want to send messages to other n-1 processes to quit

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

From where I could get handles to those other processes?


Solution

  • Please see my PostThreadMessage to Console Application.

    I created that because it certainly is possible to send a message to a console program, we just must make a message loop, just as it is possible to show a window from a console program.

    Note that PostThreadMessage needs a thread id, not a process id. Every process also has a thread id and a process's thread id is in the PROCESS_INFORMATION from CreateProcess.

    The following is a larger example but easier to use for demonstrating that PostThreadMessage works in console programs. This program will call itself (passing its thread id) if there is no argument for it then it will wait for the new process to send messages. If there is an argument then it will assume the argument is a thread id and send a message to that thread followed by a 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;
    }