Delphi uses CreateProcess to create processes and eject process PID values

Source: Internet
Author: User

Var
Exename:pchar;
Startupinfo:tstartupinfo;
Processinfo:tprocessinformation;
Begin
Fillchar (Processinfo,sizeof (tprocessinformation), 0);
Fillchar (Startupinfo,sizeof (Tstartupinfo), 0);
Startupinfo.cb:=sizeof (Tstartupinfo);
Startupinfo.dwflags:=startf_useshowwindow;
Startupinfo.wshowwindow:=sw_show;
EXEName: = ' C:\Program files\internet Explorer\iexplore.exe '; The process path created
If CreateProcess (Exename,nil,nil,nil,false,normal_priority_class,
Nil,nil,startupinfo,processinfo) Then
ShowMessage (IntToStr (PROCESSINFO.DWPROCESSID)); This is the PID value of the creation process.
End

This makes it possible to execute C:\test.exe
If the PID you want is a process ID then this
PIPROCINFOGPS.DWPROCESSID is PID.

When a thread calls CreateProcess, the system creates a process kernel object whose initial usage count is 1.
The process kernel object is not the process itself, but rather a smaller data structure used by the operating system to manage the process. You can treat a process kernel object as a small data structure that consists of the statistics of the process. The system then creates a virtual address for the new process

Space and load the code and data of the executable file or any necessary d l files into the address space of the process.
The system then creates a thread kernel object for the main thread of the new process (which uses a count of 1). Like the process kernel objects, thread kernel objects are small data structures that the operating system uses to manage threads. By executing the C + + run-time startup code, the primary

The thread starts running, and it eventually calls the WinMain, wWinMain, Main, or wmain functions. If the system successfully creates a new process and the main thread, CreateProcess returns True.
  
The Pszapplicationname and pszCommandLine parameters are used to set the name of the executable file to be used by the new process and the command-line string passed to the new process, respectively
The prototype of the pszCommandLine parameter is ptstr. This means that CreateProcess expects you to pass an address of a very literal string. Internally, CreateProcess does not actually modify the command-line string that you pass to it. However, in

Before CreateProcess returns, it restores the string to its original form.
If the command-line string is not included in the read-only portion of the file image, a violation of the access problem occurs. The best way to solve this problem is to copy the constant string into the temporary cache as follows before calling CreateProcess. When CreateProcess points

When you pszcommandline a string, it looks at the first token in the string and assumes that the tag is the name of the executable file that you want to run. If the file name of the executable does not have an extension, assume that it has an. exe extension. CreateProcess also press below

In the order in which to search for the executable file:
1) contains the calling process. The directory of E x e files.
2) Call the current directory of the process.
3) The system directory of the Wi n d o w S.
4) Wi n d o w s directory.
5) The directory listed in the PAT H environment variable.
If the file name contains a full path, the system uses the full path to view the executable file and no longer searches for these
Directory. If the system finds an executable file, it creates a new process and the code and data for the executable file
Map to the address space of the new process. The system will then call the C + + run-time startup routine.
All of this is when the Pszapplicationname parameter is null (which should be the case when more than 99%)
happen. If you do not pass NULL, you can pass the address to the Pszapplicationname parameter that contains the executable you want to run
A string of the name of the file. Note that you must set the file's extension, and the system will not automatically assume that the filename has a
An. exe extension. CreateProcess assumes that the file is in the current directory unless there is a path preceded by a file name. If the file cannot be found in the current directory, CreateProcess will not find the file in any other directory, and it fails to run.
  
You can use the psaprocess and Psathread parameters to set the security required for process objects and thread objects, respectively. , in which case the system assigns the default security descriptor to these objects. You can also specify two
A security_attributes structure and initialize them to create their own security permissions and assign them to process objects and thread objects.
Another reason to use the security_attributes structure for psaprocess and Psathread parameters is that the parent
Any child processes that are generated in the future can inherit any one of the two object handles.
  
The Fdwcreate parameter is used to identify flags that are used to specify how new processes are created.
  
The pvenvironment parameter is used to point to a block of memory containing the environment string that the new process will use. That enables a child process to inherit a set of environment strings that its parent process is using. can also
Use the Getenvironmentstrings function.
This function is used to obtain the address of the environment string data block that the calling process is using. You can use this function to return the ground
As the pvenvironment parameter of the CreateProcess. If NULL is passed for the pvenvironment parameter, then this is
Is the operation that the CreateProcess function does. When the memory block is no longer needed, the freeenvironmentstring s function should be called to release the memory block.
  
  
The Pszcurdir parameter allows the parent process to set the current drive and directory of the child process. If this parameter is NULL, the working directory of the new process will be the same as the directory of the application that generated the new process. If this parameter is not NULL, then Pszcurdi R
You must point to a 0-terminated string that contains the required work drive and working directory. Note that the drive in the path must be set
Name of the actuator.
  
  
The Psistartinfo parameter is used to point to a startupinfo structure.
When Windows creates a new process, it uses the relevant members of that structure. Most applications will require that the generated
The program uses only the default values. If the contents of the structure cannot be initialized to zero, then the members of the struct will contain any
Useless information. Passing this useless information to CreateProcess means that sometimes a new process is created, and sometimes you cannot create
The new process depends entirely on that useless information. It is important to set the unused members of the struct to zero, so that the
CreateProcess can run consistently and consistently. Failure to do so is the most common error for developers.
The Ppiprocinfo parameter is used to point to the process_information structure that you must specify. CreateProcess in
Returns the member of the struct before it is initialized.
When the process is created, the system assigns an initial usage count value of 1 to each object. Then, before CreateProcess returns, the function opens the Process object and thread object and puts the process-related handle of each object into the process_in

The hprocess and hthread members of the formation structure. When CreateProcess opens these objects internally, the usage count for each object becomes 2.
Before the system can release the process object, the process must terminate (decrements the usage count to 1), and
The parent process must call CloseHandle (and then decrement the usage count by 1 to make it 0). Similarly, to release a thread object,
The thread must be terminated and the parent process must close a handle to the thread object.
  

When a process kernel object is created, the system gives the object a unique identification number, and any other process in the system
Kernel objects cannot use this same I-D number. The same is true for thread kernel objects. When a thread kernel object is created
, the object is given a unique, system-wide I-D number. The process i d and thread i d share the same pool of numbers.
This means that processes and threads cannot have the same I D. In addition, the object is never given 0 as its I D. In
Before CreateProcess returns, it is populated with these IDs into the Dwprocessid and Dwthre Adid members of the process_information structure.
The application uses I d to track processes and threads, and must understand that the system immediately re-uses the process I d and thread i D
The system cannot remember the I d of the parent process for each process, but since I d is immediately reused, wait until the
When I d of the parent process, the I d may identify a completely different process in the system. The parent process may have terminated running. If the application wants to communicate with its creator, it is best not to use I D. You should define a more persistent
Good mechanisms, such as kernel objects and window handles.
The only way to ensure that the process I d or thread i d is not reused is to ensure that the kernel objects of the process or thread do not
Be undone.

Delphi uses CreateProcess to create processes and eject process PID values (RPM)

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.