Jul 3, 2007

Various methods for launching another program from one program VC++

There are several methods for launching another program from one program.

You can use _spawn(), _exec(), system(), WinExec(), ShellExecute(), ShellExecuteEx(), and CreateProcess().



The system() function is a more portable function.

WinExec(), ShellExecute(), ShellExecuteEx(), and CreateProcess() are unique to Windows programming. All four windows functions allow for launching an application in the background (window-hidden).



The WinExec() is the easiest of the four to use. It only takes two arguments, however MS recommends using the CreateProcess() instead of WinExec on all 32-bit programs.



The ShellExecute() is easier to setup then CreateProcess, and it has more options then WinExec. However, it's also a legacy function. MS recommends using ShellExecuteEx() for Win32 applications.

HINSTANCE ShellExecute(

HWND hwnd,

LPCTSTR lpVerb,

LPCTSTR lpFile,

LPCTSTR lpParameters,

LPCTSTR lpDirectory,

INT nShowCmd

);



The ShellExecuteEx() only takes one parameter, but the parameter is a structure with many members, and it's almost as compicate as the CreateProcess() function.

BOOL ShellExecuteEx(

LPSHELLEXECUTEINFO lpExecInfo

);





The CreateProcess() function takes 10 parameters, and can be tricky to use.

int system( const char *command );

UINT WinExec(

LPCSTR lpCmdLine, // address of command line

UINT uCmdShow // window style for new application

);

BOOL CreateProcess(

LPCTSTR lpApplicationName,

// pointer to name of executable module

LPTSTR lpCommandLine, // pointer to command line string

LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes

LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes

BOOL bInheritHandles, // handle inheritance flag

DWORD dwCreationFlags, // creation flags

LPVOID lpEnvironment, // pointer to new environment block

LPCTSTR lpCurrentDirectory, // pointer to current directory name

LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO

LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION

);

No comments: