CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2010
    Posts
    4

    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?

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile 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

  5. #5
    Join Date
    Aug 2010
    Posts
    4

    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.

  6. #6
    Join Date
    Aug 2010
    Posts
    4

    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?

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: c++ to launch a program

    Quote Originally Posted by autodale View Post
    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

  8. #8
    Join Date
    Aug 2010
    Posts
    4

    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.

  9. #9
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: c++ to launch a program

    Quote Originally Posted by autodale View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured