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).
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.
C++ example:
C# example:
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.
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).
(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).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 thanCreateProcess
!) 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:
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.