-
August 2nd, 2010, 06:07 AM
#1
c++ to launch a program
I am wondering what the capabilities of C++ are for launching executables. I would like to create a launcher to launch a program in the same folder. The launched program needs to be run maximized and in Windows 98 compatibility mode. Is this possible, and is there be a code snippet available that would show me how to do this?
-
August 2nd, 2010, 06:22 AM
#2
Re: c++ to launch a program
That cannot be done with C++, you have to use the Win32 API (which should be available since you want to start the target application in Win98 compatibility mode). Your problem can be solved with 3 steps:
1) set up an environment block for the new process. You can read the current environment settings by calling GetEnvironmentStrings and copy them to the new environment settings
2) set the __COMPAT_LAYER variable to Windows98
3) call CreateProcess with the appropriate parameters (providing the newly created environment settings)
- Guido
-
August 2nd, 2010, 06:47 AM
#3
Re: c++ to launch a program
Unless you have any extra needs, the appropriate solution would be a shortcut. Eventually a batch.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
August 2nd, 2010, 08:48 AM
#4
Re: c++ to launch a program
You could write a batch programmatically and launch it with CreateProcess
Code:
STARTUPINFO si = { 0 };
si.cb = sizeof(si);
si.showWindow = SW_HIDE;
PROCESS_INFO pi = { 0 };
std::ofstream batch("temp.bat");
batch << "@echo off" << std::endl
<< "set __COMPAT_LAYER=Win98" << std::endl
<< executable_to_invoke << std::endl;
batch.close();
CreateProcess(NULL, "call temp.bat", NULL, NULL, FALSE,
DETACHED_PROCESS, NULL, NULL, &si, &pi);
...
where 'executable_to_invoke' is a string variable that contains the filename (full path if not in working directory) of the executable you want to launch.
After CreateProcess you could wait for the process handle pi.hProcess (calling WaitForSingleObject) if your launcher should wait until execution of the batch has ended. If not, you at least may wait until the executable was launched from batch, e. g. by enumerating windows or processes until you found the newly started program. If you could retrieve the window handle of the new process that way, you also could call 'ShowWindow(hwndProc, SW_MAXIMIZE);' to get it maximized.
Regards, Alex
-
August 2nd, 2010, 09:45 AM
#5
Re: c++ to launch a program
Thanks for the input so far. I did make a batch previously which launches the program maximized, but I don't know how to set compatibility mode in a batch file... and a seemingly minor but important issue is the fact that the command prompt flashed up on the screen for a second before the program launches. This is a commercial product I am producing and that will sort of undercut the professional veneer. If I can get past those two issues somehow (compatibility mode and the flashing command prompt) I would be happy to just use a batch file for this.
-
August 2nd, 2010, 09:53 AM
#6
Re: c++ to launch a program
I have managed to grasp what you put in front of me regarding the compatibility mode now (sorry). And it works great. Any ideas about how to prevent the command prompt from briefly flashing before the program launches?
-
August 2nd, 2010, 10:14 AM
#7
Re: c++ to launch a program
Originally Posted by autodale
I have managed to grasp what you put in front of me regarding the compatibility mode now (sorry). And it works great. Any ideas about how to prevent the command prompt from briefly flashing before the program launches?
You can either omit the batch file at all and follow the approach I advised in my previous approach or specify a window coordinate where the command window cannot be seen. Use the dwX, dwY and dwFlags Attributes.
- Guido
-
August 2nd, 2010, 10:28 AM
#8
Re: c++ to launch a program
I would try what you suggested previously but it's beyond my understanding of c++. I'm just a newbie who was wondering if it could be done in a few simple lines.
-
August 2nd, 2010, 10:51 AM
#9
Re: c++ to launch a program
Originally Posted by autodale
I have managed to grasp what you put in front of me regarding the compatibility mode now (sorry). And it works great. Any ideas about how to prevent the command prompt from briefly flashing before the program launches?
Did you try the CreateProcess with the DETACHED_PROCESS creation flag? The DETACHED_PROCESS flag should prevent the console window to be shown.
Regards, Alex
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|