Windows 2000 System Programming - Creation of Process
Dai Zongyou, Yuan Guanglin, Li
(Artillery College Computer Teaching Research)
Summary: This article describes the method of Windows 2000 System Programming - Process Creation.
Key words: process, thread, handle.
.
1 Introduction
A new generation of operating systems - Windows 2000 is an environment with complete features, which is better than the same task Windows 2000 compared to other operating systems. Programmaker wants to write system hierarchical applications and user applications to master process management in Windows 2000 is critical. The following is a detailed discussion on the creation of the Windows 2000 process. In order to enable the reader to master the characteristics of Windows 2000, write more efficient and more useful programs.
2 use the Winexec command
(1) Function prototype:
UINT WIN EXEC (LPCSTR LPCMDLINE, UINT UCMDSHOW);
(2) Parameters:
LPCMDLINE: Points to an empty string, the string contains the command line of the application to be executed (file name plus an optional parameter).
Ucmdshow: Defines how Windows applications are displayed and provide the value of Wstartupinfo members for the CreateProcess function.
(3) return value:
If the function call is successful, the return value is greater than 31. If the function call fails, the return value is one of the following:
1 0: System memory or resources have been exhausted.
2 Error_bad_format: EXE file is invalid (non-Win32.exe or .exe image error).
3 Error_File_not_Found: The specified file is not found.
4 Error_Path_not_found: The specified path is not found.
Although Microsoft thinks Winexec has been outdated, but in many cases, simple Winexec functions are still the best way to run new programs. Simply transmit the command line as the first parameter, you also need to decide how to display the program (the program may ignore it) the second parameter. Typically, set it to sw_show, you can also try SW_MINIMIZED or SW_MAXIMIMIMIMIZED. Winexec does not allow all options to be obtained with CreateProcess, and it is indeed simple.
3 Use the shellexecute command
(1) Function prototype:
Hinstance Shellexecute (HWND HWND, LPCTSTR LPOPERATION, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTR LPDIRECTORY, INT NSHOWCMD);
(2) Parameters:
HWND: Points to the window handle of the parent window. This window receives any information generated by the application.
LPOPERATION: A string address of an empty end, this string specifies the operation to be executed. The following operation strings are valid:
"Open" This function opens the file specified by the parameter lpfile, which can be an executable or document file, but also a folder. "Print" This function prints the file specified by the parameter lpfile. This file should be a document file. If this file is an executable file, this file is opened. "Explore" This function searches for the folder specified by the parameter lpfile, this file should be a document file,
This parameter can be empty. In this case, the function is used to open the file specified by the parameter lpfile.
LPFILE: A string address of an empty end, this string specifies the file you want to open or printed or the folder to open or search.
LPParameters: If the parameter lpfile specifies an executable file, LPParameters is an empty string address that specifies the parameters to be passed to the application. If lpfile specifies a document file, LPParameters should be empty.
LPDIRECTORY: A string address of an empty end, this string specifies the default directory.
nshowcmd: If lpfile specifies an executable, nshowcmd indicates how the application is turned on. If lpfile specifies a document file, NSHOWCMD should be empty.
(3) return value:
If the function call is successful, the return value is greater than 32, otherwise it is an error value that is less than or equal to 32.
Description: You can use this function to open or search for a housing folder. Open a folder available in any of the following:
Shellexecute (Handle, Null, Path_to_Folder, Null, Null, SW_SHOWNORMAL);
or
Shellexecute (Handle, "Open", Path_to_Folder, Null, NULL, SW_SHOWNORMAL);
Search folders, available as follows
Shellexecute (Handle, "Explore", Path_to_Folder, Null, Null, SW_SHOWNORMAL
Although the shellexecute command has been out of time but easy to get. This command presents the command interpreter to open, browse, or print a document or folder, although you can run the program with the shellexecute, but usually only send a document name, and the command interpreter decides to run the program. In addition, when open the directory folder, the shellexecute command is very useful.
⑷ ⑷ ⑷ 示
By following the use of Winexec and Shellexecute. The following program has a console program example that uses two different methods to open a text file. The following program uses Winexec and explicitly specifies the use of Notepad. Then, use Shellexecute to open the text file.
Program list
#include
#include
Void main (int Argc, char * argv [])
{
COUT << "Opening with Winexec / N";
IF (Winexec ("Notepad Readme.txt", Sh_Show) <32)
MessagBox (NULL, "Can't Winexec", NULL, MB_OK;
COUT << "press enter / n";
MessagBox (NULL, "Press Ok to Continue", "ProgRM Launched", MB_OK;
COUT << "Opening with shellexecute / n";
IF (SHELLECUTE (NULL, "Open",
"Readme.txt", null, null, sw_show) <(Handle) 32)
MessagBox (NULL, "Can't Shellexecute / N", NULL, MB_OK;
}
4 Using the createProcess command
(1) Function prototype:
Bool createprocess
LPCTSTR LPAPPLICATIONNAME,
LPTSTR LPCOMMANDLINE,
LPSecurity_attributes LPPROCESSATTRIBUTES,
LPSecurity_attributes LPTHREADATIADATTRIBUTES,
Bool binherithandles,
DWORD DWCREATIONFLAGS,
LPVOID LPENVIRENT, LPCTSTR LPCURRENTDIRECTORY,
LPStartupinfo LPstartupinfo,
LPPROCESS_INFORMATION LPPROCESSINFORMATION
);
(2) Parameters:
LPApplicationName: Points a string with an empty end, and he specifies the module to be executed.
LPCommandline: Points a string with an empty end that defines the command line to be executed.
LPPROCESSATTRIBUTES: Points to a security_attributes structure that determines whether the returned handle is inherited.
LPTHREADATTRIBUTES: Points to a Security_Attributes structure that determines whether the returned handle can be inherited.
BinheritHandles ,: Indicates whether the new process inherits the handle from the calling process.
DWCREATIONFLAGS: Defines the additional flags created by the control priority and process.
LpenVironment: Points to a new process.
LpCurrentDirectory: Points a string with an empty end that defines the current drive and current directory of the child process.
LPSTARTUPINFO: Points to a StartupInfo structure that defines how the new process will be displayed.
LPPROCESSINFORMATION: Points to the Process_information structure, which accepts the representation information about the new process.
(3) return value:
If the function call is successful, the return value is not 0; if the function call fails, the return value is 0.
SHELLEXECUTE and WINEXEC commands are used for simple jobs. If you want to completely control a new process, you must call CREATEPROCESS.
In the above parameters, the parameter LPStartupInfo is the STARTUPINFO structure. Can be used to set the title of the console, the initial size and position of the new window, and redirect standard input and output. New programs can usually ignore most of these data items, if you do that. The flags in the structure can be stipulated that the data segment to be set has been shown. Sometimes, you don't want to set any information, you must also pass an effective pointer to the empty structure (determine the setting size to CB, and set DWFLAGS members 0). Parameters LPPROCESSINFORMATION Returns the process and thread handles, as well as process and thread ID. These handles have access specified in parameters LPProcessAttributes and LPTHREADATIATRIBUTES.
Note that some parameters for CreateProcess are specific to console applications, while other parameters are useful for various applications. In most cases, it is not necessary to fill in the STARTUPINFO structure, but it must be provided anyway. Its return value is Boolean, and the return value of the truly interested interest occurs in the structure of the parameter transfer (Process_information). CreateProcess returns the process ID and its handle, and the initial thread ID and its handle. You can send ID to other processes, or use your handle to control new processes.
⑷ related commands
For a given process handle, you can use the relevant command to control the process. Below we discuss the determination of the end of the process, the determination of the end of the process has the following methods:
1 call getExitcodeProcess
Command getExitCodeProcess can return to STILL_ACTIVE, or return the process exit value (if the process is completed) returns a pointer, which points to the command filled.
2 Call the WAITFORSINGLEOBJECTWAITFORSINGLORSINGLEOBJECT is to determine if the handle is in the state of the transmitted signal. When the process ends, the process handle issues a signal. When the WaitForsingleObject is called, the process handle and timeout value are specified, if the time is 0, the command returns, and the state of the process can be determined. If the timeout is constant infinite, the command will not return until the target process exits. Of course, you can also specify a timeout value that causes the command to wait for a period of time. If the process ends at the time of the timeout, the command returns and indicates that the handle is transmitted in the transmitted signal. Otherwise, a negative value is returned. WaitforsingleObject will be successfully returned regardless of the state of the handle, and there is no error. To determine the state of the process, the return value must be compared to WAIT_Object_0 (signal) and Wait_Timeout (not signal). The real error return value is Wait_Failed. In addition, possible return values is WAIT_ABANDONED, and will not see when the process is processed. To wait a process, you must have an open attire of the SYNCHRONIZE privilege.
It should be noted here that the process ID is different from the process handle. The handle cannot be transmitted between the processes, which means that one process cannot be manipulated directly from the external process unless there is a handle. However, the OpenProcess command will allow any program (sufficient security privilege) to convert the process marker (can be used to other processes) into process handles. You can also understand the current process marker by calling getCurrentProcessID. This is very useful if you want to share with other unrelated processes so that you can open the process handle. But when calling OpenProcess, access to the process can be requested. Access to each process, maybe or may have no access to the security to open the process, so that the request request is only required. For example, if you want to understand the return code of the process, you need access to Process_Query_information. To terminate the process, you must have access to Process_Terminate.
⑸ ⑸ ⑸ 示
The following is used in an example to use createProcess and related commands. The following program is two simple console applications, the first program (MASTER) runs the second program (slave) and enters sleep. The SLAVE program reads the process ID (PID) of the command line, and waits for the master program to terminate. These programs illustrate the following important technologies:
l Using CreateProcess
l Using OpenProcess
l Use WaitForsingleObject
Program List Master program
#include
#include
#include
#include
Void main (int Argc, char * argv [])
{
CHAR CMD [128];
IF (argc! = 1)
STRCPY (CMD, Argv [1]);
Else
STRCPY (CMD, "Slave.exe");
INT pid = getcurrentprocessid ();
Sprintf (CMD Strlen (CMD), "% D", PID);
COUT << "master: start:" << cmd << "/ n");
Cout.flush ();
STARTUPINFO INFO;
MEMSET (& INFO, 0, SIZEOF (INFO); info.cb = sizeof (info);
PROCESS_INFORMATION PINFO;
IF (! CreateProcess (NULL, CMD, NULL, NULL, FALSE { C Out << "Master: Slave Process Did Not Start / N"; C Out << "Master: Try Naming Slave Process on The Command Line / N"; } COUT << "Master: Sleeping / N"; Cout.flush (); SLEEP (15000); Cout << "Master: EXITING / N"; exit (0); } Program list SLAVE program #include #include #include Void main (int Argc, char * argv []) { IF (argc! = 2) { Cout << "Slave: Please run master.exe instead./N"; Exit (1); } INT PID = ATOI (Argv [1]); Handle process = OpenProcess (Process_Query_Information | Synchronize, False, PID); IF (! process) COT << "Slave: Error Opening Process / N"; Cout << "Slave: waiting for master to finish / n"; Cout.flush (); IF (WaitForsingleObject (Process, Infinite) == Staute_Wait_0) Cout << "slave: master completed / n"; Else COUT << "slave: undexpected error / n"; exit (0); } 5 Summary: By introducing this article, the reader should master the creation of the Windows 2000 process and understand the control of the Windows 2000 process. references 1 "New WINDOWS API Reference Daquan" Zhu Youqin editor ............ Electronic Industry Press, 2000. 2 "Windows 2000 System Programming" (US) AI Williams Board Wellwen Wenliang and other translations ... Machinery Industry Press, 2001.