views:

29

answers:

1

I got two console applications that the first one runs the second one:

1_first console application:

#include <Tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;

void main(){
 PROCESS_INFORMATION obj1;
 memset(&obj1,0,sizeof(PROCESS_INFORMATION));
 STARTUPINFOW obj2;
 memset(&obj2,0,sizeof(STARTUPINFOW));
 obj2.cb=sizeof(STARTUPINFOW);
 CreateProcessW(_TEXT("c:\\runme.exe"),_TEXT("hello what's up?"),NULL,NULL,FALSE,NULL,NULL,NULL,&obj2,&obj1);
}

2_second console application named runme.exe:

#include <stdio.h>
#include <iostream>
using namespace std;

int main(int argc,char * * argv){
 if (argc>0)
  for (int i=0;i<argc;i++)
   cout <<"**->**"<<argv[i]<<"\n";
}

Now my problem is that both applications will use the same command prompt window, what should I do to get them using separate ones ?

+3  A: 

Pass CREATE_NEW_CONSOLE in the process creation flags (sixth parameter) when you call CreateProcess.

CreateProcessW(L"c:\\runme.exe",L"hello what's up?",NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&obj2,&obj1);

When you call CreateProcessW you do not want to use _TEXT on strings. CreateProcessW always takes wide strings, so you should always use an L prefix on them. _TEXT (or _T) is only for use with CreateProcess (no suffix), so it can change from narrow to wide strings based on whether you define UNICODE/_UNICODE.

Jerry Coffin