0

I'm trying to write an application to open another and self close, so I don't need the console when the external application opens

This is what I've tried so far:

system("cmd.exe /c application.exe"); //console shows, application opens, console wait

system("start \"\" application.exe"); //console shows, application opens, console close

//console does not show but neither the application (I can see it in task manager)
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb           = sizeof(si);
si.dwFlags      = STARTF_USESHOWWINDOW;
si.wShowWindow  = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));
CreateProcess(0, "application.exe", 0, 0, FALSE, 0, 0, 0, &si, &pi);

//console does not show but neither the application (I can see it in task manager)
WinExec("application.exe", SW_HIDE);

This is the way I compile:

g++ -o "launcher" "launcher.cpp" -mwindows
shuji
  • 7,369
  • 7
  • 34
  • 49
  • small question: have you checked that the compilation works and you have an 'application.exe' ? – Marco A. Jul 22 '14 at 18:07
  • Yes, application opens with each command. The problem is that the console is showing up and sometimes stays opened – shuji Jul 22 '14 at 18:08
  • Also: make sure your app isn't just exiting as soon as it is executed. `CreateProcess` is an asynchronous command and will exit as soon as your app is launched – Marco A. Jul 22 '14 at 18:11
  • the process is correctly created (is executing in background) but is hidden and I dont know what happens with the launcher but has to close after this anyway. – shuji Jul 22 '14 at 18:13

1 Answers1

1

This is some code that works for me to accomplish your goal:

// Declare and initialize process blocks
PROCESS_INFORMATION processInformation;
STARTUPINFO startupInfo;

memset(&processInformation, 0, sizeof(processInformation));
memset(&startupInfo, 0, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);

// Call the executable program
TCHAR cmd[] =  myCommandText;

int result = ::CreateProcess(NULL, cmd, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, NULL, NULL, &startupInfo, &processInformation);

In this context, myCommandText is a console command

Logicrat
  • 4,438
  • 16
  • 22