Today, let's continue to talk about C # calling the system API to start the external Program Method, today we are talking about the CreateProcess API function, compared to the first two Article (1 and 2) the CreateProcess parameter is more complex, but more flexible to use.
1. Using System. runtime. interopservices;
2. Several struct types are used in CreateProcess. declare them first:
[System. runtime. interopservices. structlayout (layoutkind. Sequential)]
Public Class Security_attributes
{
Public Int Nlength;
Public String Lpsecuritydescriptor;
Public Bool Binherithandle;
}
[Structlayout (layoutkind. Sequential)]
Public Struct Startupinfo
{
Public Int CB;
Public String Lpreserved;
Public String Lpdesktop;
Public Int Lptitle;
Public Int Dwx;
Public Int Dwy;
Public Int Dwxsize;
Public Int Dwysize;
Public Int Dwxcountchars;
Public Int Dwycountchars;
Public Int Dwfillattribute;
Public Int Dwflags;
Public Int Wshowwindow;
Public Int Cbreserved2;
Public Byte Lpreserved2;
Public Intptr hstdinput;
Public Intptr hstdoutput;
Public Intptr hstderror;
}
[Structlayout (layoutkind. Sequential)]
Public Struct Process_information
{
Public Intptr hprocess;
Public Intptr hthread;
Public Int Dwprocessid;
Public Int Dwthreadid;
}
3. Declare CreateProcess
[Dllimport ( " Kernel32.dll " , Charset = Charset. ANSI)]
Public Static Extern Bool CreateProcess (
Stringbuilder lpapplicationname, stringbuilder lpcommandline,
Security_attributes lpprocessattributes,
Security_attributes lpthreadattributes,
Bool Binherithandles,
Int Dwcreationflags,
Stringbuilder lpenvironment,
Stringbuilder lpcurrentdirectory,
Ref Startupinfo lpstartupinfo,
Ref Process_information lpprocessinformation
);
4. The three below are also APIs. For more information, see annotations.
# Region Win32 API: waitforsingleobject
// Detect the Signal Status of a system core object (thread, event, signal). When the object execution time exceeds dwmilliseconds, it returns; otherwise, it waits until the object returns a signal.
[Dllimport ("kernel32.dll")]
Public static extern uint waitforsingleobject (system. intptr hhandle, uint dwmilliseconds );
# Endregion
# Region Win32 API: closehandle
// Close a kernel object and release the system resources occupied by the object. This includes file, file ing, process, thread, security, and synchronization objects.
[Dllimport ("kernel32.dll")]
Public static extern bool closehandle (system. intptr hobject );
# Endregion
# Region Win32 API: getexitcodeprocess
// Obtain the exit of an interrupted processCode, Non-zero indicates success, and zero indicates failure.
// The hprocess parameter. To obtain the handle of a process that exits the code, the lpexitcode parameter is used to load a long integer variable of the process exit code.
[Dllimport ("kernel32.dll")]
Static extern bool getexitcodeprocess (system. intptr hprocess, ref uint lpexitcode );
# Endregion
5. If the execution time of the program specified by argm In the example is long, the process will be blocked to the waitforsingleobject line until the command execution is complete or the process is aborted.
String Argm = " Cmd.exe " ;
Startupinfo SINFO= NewStartupinfo ();
Process_information pinfo= NewProcess_information ();
If ( ! CreateProcess ( Null , New Stringbuilder (argm ), Null , Null , False , 0 , Null , Null , Ref SINFO, Ref Pinfo ))
{
Throw New Exception ( " Call failed " );
}
Uint I = 0 ;
Waitforsingleobject (pinfo. hprocess, Int . Maxvalue );
Getexitcodeprocess (pinfo. hprocess, Ref I );
Closehandle (pinfo. hprocess );
Closehandle (pinfo. hthread );