'system()' family (C run-time library - ANSI ('system()') or Win NT ('_wsystem()'))
'_exec()' family (C run-time library - Win 95, Win NT)
'_spawn()' family (C run-time library - Win 95, Win NT)
'WinExec()' (Win32 API)
'ShellExecute()' (Shell API)
'ShellExecuteEx()' (Shell API)
'CreateProcess()' (Win32 API)
'CreateProcessAsUser()' (Win32 API)
'CreateProcessWithLogonW()' (Win32 API)
Most common are 'ShellExecute()', ShellExecuteEx()' and 'CreateProcess()'. Note that 'WinExec()' is provided only for compatibility with 16-bit Windows and should not be used any longer. The following examples will show how to display the text file 'c:\example.txt' in 'notepad.exe' using these three functions...
'ShellExecute()'
Code:
HINSTANCE hInst = ShellExecute(0,
"open", // Operation to perform
"c:\\windows\\notepad.exe", // Application name
"c:\\example.txt", // Additional parameters
0, // Default directory
SW_SHOW);
if(reinterpret_cast<int>(hInst) <= 32)
{
// Could not start application
switch(reinterpret_cast<int>(hInst))
{
case 0:
// The operating system is out of memory or resources.
break;
case ERROR_FILE_NOT_FOUND:
// The specified file was not found.
break;
case ERROR_PATH_NOT_FOUND:
// The specified path was not found.
break;
case ERROR_BAD_FORMAT:
// The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image).
break;
case SE_ERR_ACCESSDENIED:
// The operating system denied access to the specified file.
break;
case SE_ERR_ASSOCINCOMPLETE:
// The file name association is incomplete or invalid.
break;
case SE_ERR_DDEBUSY:
// The Dynamic Data Exchange (DDE) transaction could not be completed because
// other DDE transactions were being processed.
break;
case SE_ERR_DDEFAIL:
// The DDE transaction failed.
break;
case SE_ERR_DDETIMEOUT:
// The DDE transaction could not be completed because the request timed out.
break;
case SE_ERR_DLLNOTFOUND:
// The specified dynamic-link library (DLL) was not found.
case SE_ERR_FNF:
// The specified file was not found.
break;
case SE_ERR_NOASSOC:
// There is no application associated with the given file name extension.
// This error will also be returned if you attempt to print a file that is
// not printable.
break;
case SE_ERR_OOM:
// There was not enough memory to complete the operation.
break;
case SE_ERR_PNF:
// The specified path was not found.
break;
case SE_ERR_SHARE:
// A sharing violation occurred.
break;
}
}
In general 'ShellExecuteEx()' and even 'CreateProcess()' provides more controlling features of the application to start and its termination as shown in the FAQ 'How can I wait until a process ends?'
Last edited by Andreas Masur; July 21st, 2005 at 04:30 PM.
Bookmarks