Does anyone know what is the C function i can use to call dos shell commands? For example, i want to use my C program to execute "c:\windows\notepad.exe"?
Also, you can use exec and spawnl. They are useful if you need to specify command line parameters for the process you want to start, or even to change environment settings for it.
The differences between:
1) system, exec, spawnl and
2) CreateProcess
- methods from 1) are obsolete (DOS & console programming - can be run under DOS and Win);
- CreateProcess, besides it creates a new process it enables you to define security attributes for that process to create it suspended and run it whenever you need to, change its error mode, environment block, process' main window location (station, desktop, and position on screen). Very important is that it allows you to redirect standard input, output and error to whatever handle you might need.
- moreover, CreateProcess fills (as an [out] parameter) an user-supplied PROCESS_INFORMATION structure which contains handles to the process & its primary thread and also process ID and primary thread ID (thus enabling you to manipulate the newly created process by using PSAPI, for example)
Yet another difference:
Only system() is C++ Standard library routine (it's not an obsolete.
Both exec() and spawn() family routines are to be found in (almost) all platforms under slightly different names (they came from the Unix world).
CreateProcess() is Windows API only routine.
each call of system("xxx") resumes on some OS specific API. With my experience on windows and on linux programming I never had to call function system. However this function is standard and portable, instruction what you pass to that function are not very portable. So, to create a new executable file based process in a more portable fasion better is to call exec* family functions. But when using some OS always is better to take advantages of OS are you using.
> i want to use my C program to execute "c:\windows\notepad.exe"?
I recommend using CreateProcess, since it looks like you don't want portability on other OS that Windows. It gives you more control over the started application (see my previous post).
But for CreateProcess, i have included windows.h but it seems that i am unable to properly link the program. Do i have to use any specific compiler to do so? Seems that the function CreateProcess does not exist when i tried to link.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.