views:

1170

answers:

4

I would like to call a windows program within my code with parameters determined within the code itself.

I'm not looking to call an outside function or method, but an actual .exe or batch/script file within the WinXP environment.

C or C++ would be the preferred language but if this is more easily done in any other language let me know (ASM, C#, Python, etc).

+6  A: 

I think you are looking for the CreateProcess function in the Windows API. There are actually a family of related calls but this will get you started. It is quite easy.

Mark Brittingham
Well, it's not as easy as using system() but at least such a solution exists! Thanks for the strongest suggestion and link.
Mr. H.
+3  A: 

One of the simplest ways to do this is to use the system() runtime library function. It takes a single string as a parameter (many fewer parameters than CreateProcess!) and executes it as if it were typed on the command line. system() also automatically waits for the process to finish before it returns.

There are also limitations:

  • you have less control over the stdin and stdout of the launched process
  • you cannot do anything else while the other process is running (such as deciding to kill it)
  • you cannot get a handle to the other process in order to query it in any way

The runtime library also provides a family of exec* functions (execl, execlp, execle, execv, execvp, more or less) which are derived from Unix heritage and offer more control over the process.

At the lowest level, on Win32 all processes are launched by the CreateProcess function, which gives you the most flexibility.

Greg Hewgill
This is definitely what I thought I was looking for...but those limitations are pretty hard to get by...I might do it if the design will work with it. Thanks for this info!
Mr. H.
+2  A: 

C++ example:

char temp[512];
sprintf(temp, "command -%s -%s", parameter1, parameter2);
system((char *)temp);

C# example:

    private static void RunCommandExample()
    {
        // Don't forget using System.Diagnostics
        Process myProcess = new Process();

        try
        {
            myProcess.StartInfo.FileName = "executabletorun.exe";

            //Do not receive an event when the process exits.
            myProcess.EnableRaisingEvents = false;

            // Parameters
            myProcess.StartInfo.Arguments = "/user testuser /otherparam ok";

            // Modify the following to hide / show the window
            myProcess.StartInfo.CreateNoWindow = false;
            myProcess.StartInfo.UseShellExecute = true;
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;

            myProcess.Start();

        }
        catch (Exception e)
        {
            // Handle error here
        }
    }
Rorzilla
Great examples, thanks!
Mr. H.
+5  A: 

When you call CreateProcess(), System(), etc., make sure you double quote your file name strings (including the command program filename) in case your file name(s) and/or the fully qualified path have spaces otherwise the parts of the file name path will be parsed by the command interpreter as separate arguments.

system("\"d:some path\\program.exe\" \"d:\\other path\\file name.ext\"");

For Windows it is recommended to use CreateProcess(). It has messier setup but you have more control on how the processes is launched (as described by Greg Hewgill). For quick and dirty you can also use WinExec(). (system() is portable to UNIX).

When launching batch files you may need to launch with cmd.exe (or command.com).

WinExec("cmd \"d:some path\\program.bat\" \"d:\\other path\\file name.ext\"",SW_SHOW_MINIMIZED);

(or SW_SHOW_NORMAL if you want the command window displayed ).

Windows should find command.com or cmd.exe in the system PATH so in shouldn't need to be fully qualified, but if you want to be certain you can compose the fully qualified filename using CSIDL_SYSTEM (don't simply use C:\Windows\system32\cmd.exe).

Roger Nelson
These are wonderful tips for possible future obstacles and general clarification. Thanks a lot.
Mr. H.