Program: how to start a process

Source: Internet
Author: User
Program: how to start a process
Author: Deng Tao
Time: 2010-12-18, 20: 23: 40
Chain: http://bbs.pediy.com/showthread.php? T = 126748

There are several ways to start a process:
'_ Exec ()' belongs to (C Runtime function)
'_ Spawn ()' also belongs to (C Runtime function)
'Winexec () '(Win32 API)
'Shellexecute () '(Shell API)
'Shellexecuteex () '(Shell API)
'Createprocess () '(Win32 API)
'Createprocessasuser () '(Win32 API)
'Createprocesswithlogonw () '(Win32 API)
The most common ones are 'shellexecute () ', shellexecuteex ()' and 'createprocess ()',
Note that 'winexec () 'is only compatible with 16 Windows systems and should not be used any more.
The following example shows how to use the 3rd member to display the text file 'C: \ example.txt 'in 'notepad.exe '.
You can change the additional parameter to your own DLL address.
'Shellexecute ()'
Code:
Hinstance hinst = ShellExecute (0,
"Open", // The operation executed
"C: \ Windows \ notepad.exe", // name of the application
"C: \ example.txt", // additional parameter,
0, // default directory
Sw_show );
If (reinterpret_cast <int> (hinst) <= 32)
{
// The application cannot be started.
Switch (reinterpret_cast <int> (hinst ))
{
Case 0:
// The operating system has insufficient memory or resources.
Break;
Case error_file_not_found:
// The specified file is not found ,.
Break;
Case error_path_not_found:
// The specified path is not found.
Break;
Case error_bad_format:
// The. exe file is invalid (not a-Microsoft Win32. EXE or an incorrect. EXE image ).
Break;
Case se_err_accessdenied:
// The operating system rejects access to the specified file.
Break;
Case se_err_associncomplete:
// The file name is incomplete or invalid.
Break;
Case se_err_ddebusy:
// Dynamic data (DDE) processing may fail
// Because another DDE is in the processing queue.
Break;
Case se_err_ddefail:
// DDE processing failed.
Break;
Case se_err_ddetimeout:
// DDE processing may fail to complete the request timeout.
Break;
Case se_err_dllnotfound:
// The specified dynamic link library (DLL) is not found.
Case se_err_fnf:
// The specified file is not found.
Break;
Case se_err_noassoc:
// No corresponding file has the specified extension.
// If you try to print the file, this error will also be returned
// Not printable.
Break;
Case se_err_oom:
// There is not enough memory to complete the operation.
Break;
Case se_err_pnf:
// The specified path is not found.
Break;
Case se_err_share:
// Share conflict occurs.
Break;
}
}
'Shellexecuteex ()'

Code:
Shellexecuteinfo executeinfo;
Memset (& executeinfo, 0, sizeof (executeinfo ));
Executeinfo. cbsize = sizeof (executeinfo );
Executeinfo. fmask = 0;
Executeinfo. hwnd = 0;
Executeinfo. lpverb = "open"; // executed operation
Executeinfo. lpfile = "C: \ WINDOWS \ notepad.exe"; // Application name
Executeinfo. lpparameters = "C: \ example.txt"; // additional parameters
Executeinfo. lpdirectory = 0; // default directory
Executeinfo. nshow = sw_show;
Executeinfo. hinstapp = 0;
If (shellexecuteex (& executeinfo) = false)
// If the application cannot be started-> call 'getlasterror ()'

'Createprocess ()'

Code:

Startupinfo sistartupinfo;
Process_information piprocessinfo;
Memset (& sistartupinfo, 0, sizeof (sistartupinfo ));
Memset (& piprocessinfo, 0, sizeof (piprocessinfo ));
Sistartupinfo. cb = sizeof (sistartupinfo );
If (CreateProcess ("C: \ WINDOWS \ notepad.exe", // Application name
"Example.txt", // additional parameters
0,
0,
False,
Create_default_error_mode,
0,
0, // working directory
& Sistartupinfo,
& Piprocessinfo) = false)
// If the application cannot be started-> call 'getlasterror ()'
In general, 'shellexecuteex () 'and even 'createprocess ()' Provide more application startup and termination control features of the application, in the FAQ, how can I wait until a process ends? '
[Andreas Masur]
Source E: http://www.codeguru.com/forum/showthrea... Did = 231233

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.