zoukankan      html  css  js  c++  java
  • windows下开多个CMD窗口多个进程输出

    题目有点拗口,不知道大家是否明白,具体的需求就是:

    我有一个Helloworld程序,cout helloworld到标准输出,我想在一个主程序里面创建多个进程,每个进程开一个窗口,显示各自的输出。

    大概就是这么个意思,关键在百度和谷歌上关于这个问题的说明都有点语焉不详,谷歌的E文的结果也不是很详细,但是还是有些提示性的东西(毕竟老外人多,

    整好E对开发还是很重要),于是自己试了试,算是比较满意了,这里总结下,给需要的朋友参考。

    用于输出的程序可以很简单,这里就一句cout << "Hello World!"<<endl;

    当然你可以根据你的需要做多事很情,然后各种cout。

     1 #include "stdafx.h"
     2 
     3 using namespace std;
     4 
     5 int _tmain(int argc, _TCHAR* argv[])
     6 {
     7     cout << "Hello World!"<<endl;
     8     system("PAUSE");
     9     return 0;
    10 }

    主程序里面,可以采用两种不同的方式来打开窗口,一种是ShellExecuteEx,一种是CreateProcess.

    需要注意的是CreateProcess,必须给他传一个CREATE_NEW_CONSOLE参数,不然helloworld显示在跟主程序在同一个窗口里面,这就不是我要的效果了。

     1 #include "stdafx.h"
     2 #include <Windows.h>
     3 
     4 
     5 int _tmain(int argc, _TCHAR* argv[])
     6 {
     7 
     8   //Using ShellExecuteEx  
     9   SHELLEXECUTEINFO sei;  
    10     
    11   SecureZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));  
    12     
    13   sei.cbSize = sizeof(SHELLEXECUTEINFO);  
    14   sei.lpVerb = L"open";  
    15   sei.lpFile = L"cmd";  
    16   sei.fMask = SEE_MASK_NOCLOSEPROCESS;  
    17   sei.lpParameters = L"/k E:\\work\\logAnalies\\multiCmd\\HelloWorld\\Debug\\HelloWorld.exe";  
    18   sei.nShow = SW_SHOW;  
    19  
    20   ShellExecuteEx( &sei );   
    21  
    22   //WaitForSingleObject(sei.hProcess, INFINITE);    
    23  
    24   printf("Process with ID:%i has exited.\n", GetProcessId(sei.hProcess));
    25 
    26   // Using CreateProcess
    27   STARTUPINFO si;  
    28   SecureZeroMemory(&si, sizeof(STARTUPINFO));  
    29  
    30   si.cb = sizeof(STARTUPINFO);   
    31   
    32  
    33   PROCESS_INFORMATION pi;  
    34  
    35   BOOL result = CreateProcess(  
    36                       L"c:\\windows\\system32\\cmd.exe",  
    37                       L"/k E:\\work\\logAnalies\\multiCmd\\HelloWorld\\Debug\\HelloWorld.exe",  
    38                       NULL,  
    39                       NULL,  
    40                       FALSE,  
    41                       CREATE_NEW_CONSOLE,  
    42                       NULL,  
    43                       NULL,  
    44                       &si,  
    45                       &pi);  
    46  
    47   if(result)  
    48   {  
    49     WaitForSingleObject(pi.hProcess, INFINITE);  
    50     printf("Process with ID: %i has exited.\n", GetProcessId(pi.hProcess));  
    51     CloseHandle(pi.hProcess);  
    52   }  
    53 
    54   system("PAUSE");
    55     return 0;
    56 }

    开多个控制台窗口用于各个进程的监控,关键点我觉得是要明白windows的控制台其实是一个exe程序,所以createprocess或者

    ShellExecuteEx的时候,是创建cmd.exe命令,然后让cmd去执行我们的helloworld.exe程序。
    补充一点的是cmd.exe 后面跟着的/k 参数,意思是执行完后cmd并不关闭,如果是/c 则表示关闭。

    希望对各位tx有用!

    .

  • 相关阅读:
    POJ 3126 Prime Path
    POJ 2429 GCD & LCM Inverse
    POJ 2395 Out of Hay
    【Codeforces 105D】 Bag of mice
    【POJ 3071】 Football
    【POJ 2096】 Collecting Bugs
    【CQOI 2009】 余数之和
    【Codeforces 258E】 Devu and Flowers
    【SDOI 2010】 古代猪文
    【BZOJ 2982】 combination
  • 原文地址:https://www.cnblogs.com/linbirg/p/2815471.html
Copyright © 2011-2022 走看看