2016-04-17 42 views
1
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個procesess找到字符串中這樣給定的文件,並在我的模塊(wchich會在文件中給出的字符串)我想發送消息給其他N-1處理退出發送信息給其它工藝

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

從哪裏我可以得到處理這些其他進程?

+0

這不應該編譯,您不會將正確的參數傳遞給['swprintf_s'](https://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx)。 –

+0

至於你的問題,你有沒有想過最後一個參數['CreateProcess'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85% 29.aspx)是爲了?它在['PROCESS_INFORMATION'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684873%28v=vs.85%29.aspx)結構中設置了哪些信息?無論如何,這是一個好的開始。 –

+0

@JoachimPileborg我只是沒有顯示完整的代碼,我會讀這個'PROCESS_INFORMATION'感謝。 – Cardano

回答

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; 
} 
+0

[爲什麼PostThreadMessage發佈的消息消失?](https://blogs.msdn.microsoft.com/oldnewthing/20090930-00/?p=16553) – IInspectable

+0

@IInspectable,你認爲這在某種程度上是相關的嗎?我沒有看到相關性。 – user34660

+0

由於您推薦使用PostThreadMessage,因此我發佈了一篇文章鏈接,解釋了此API調用停止可靠的原因和時間。我認爲這在某種程度上是相關的。 – IInspectable