Win32 process creation, process snapshot, process termination use case

Source: Internet
Author: User

 Process creation:
1#include <windows.h>2#include <stdio.h>3 4 intMain ()5 {6 //Create open system with Notepad process7Startupinfo SI1 = {sizeof(SI1)};8 process_information pi1;9 Char* SzCmdLine1 ="Notepad";Ten if(:: CreateProcess (NULL, SZCMDLINE1, NULL, NULL, FALSE, NULL, NULL, NULL, &AMP;SI1, &pi1)) Oneprintf"Create Notepad process successfully!\n"); A - - //Create a new CMD process window the Char* SzCmdLine2 ="cmd"; -Startupinfo Si2 = {sizeof(SI2)}; - process_information pi2; -Si2.dwflags = Startf_useshowwindow;//specifies that the Wshowwindow member is valid +Si2.wshowwindow = TRUE;//If this member is set to true, the main window of the new process is displayed, False does not display - +BOOL BRet =:: CreateProcess ( ANull//do not specify the file name of the executable here atSzCmdLine2,//command-line arguments -Null//Default Process Security -Null//Default Thread Security -FALSE,//specifies that the handle within the current process cannot be inherited by the quilt process -Create_new_console,//creates a new console window for the new process and, if NULL, does not create a new window -Null//environment variables that use this process inNull//drives and directories that use this process -&SI1, to&pi2); + - if(BRet) the { * //since you don't use two handles, it's best to turn them off immediately $ :: CloseHandle (Pi2.hthread);Panax Notoginseng :: CloseHandle (pi2.hprocess); -printf"Process ID Number of the new process:%d\n", pi2.dwprocessid); theprintf"main thread ID number of the new process:%d\n", Pi2.dwthreadid); + } A the return 0; +}

Operating effect:

Process Snapshot
1#include <windows.h>2#include <tlhelp32. H>//declaring a header file for a snapshot function3#include <stdio.h>4 5 intMain ()6 {7 PROCESSENTRY32 pe32;8Pe32.dwsize =sizeof(PE32);9 Ten //take a snapshot of all processes within the system-the function is used to get a snapshot of the system-specified process, or you can pass in different parameters to get a snapshot of the heap, module, and thread used by these processes OneHANDLE Hprocesssnap =:: CreateToolhelp32Snapshot (Th32cs_snapprocess,0); A if(Hprocesssnap = =Invalid_handle_value) - { -printf"CreateToolhelp32Snapshot call failed! \ n"); the return-1; - } - - //traverse a process snapshot to display information for each process in turn +BOOL bmore =::P Rocess32first (Hprocesssnap, &pe32); - while(bmore) + { Aprintf"process Name:%s\n", pe32.szexefile); atprintf"Process ID Number:%u\n\n", pe32.th32processid); -Bmore =::P rocess32next (Hprocesssnap, &pe32); - } - - //don't forget to clear out the snapshot object - :: CloseHandle (HPROCESSSNAP); in return 0; -}

notes:
The CreateToolhelp32Snapshot is used to obtain a snapshot of the specified process within the system, or to take a snapshot of the heap, module, and thread used by these processes. The specific usage of the function is.
HANDLE WINAPI CreateToolhelp32Snapshot (
DWORD dwFlags,//used to specify the object to be returned in the snapshot, which can be th32cs_snapprocess, etc.
DWORD TH32PROCESSID//A process ID number that specifies the snapshot of which process to get, which can be set to 0 when getting a list of system processes or getting a snapshot of the current process
);
This function can not only get a list of processes, but also a list of objects such as threads and modules. The DwFlags parameter specifies the type of the obtained list, whose value can be:
Th32cs_snapheaplist enumerates the heap in the process specified by the Th32processid parameter.
Th32cs_snapmodule enumerates the modules in the process specified by the Th32processid parameter.
Th32cs_snapprocess enumerates the system-wide processes at which time the Th32processid parameter is ignored.
Th32cs_snapthread enumerates the system-wide threads at which time the Th32processid parameter is ignored.
the successful execution of the function returns a snapshot handle, otherwise returns INVALID_HANDLE_VALUE (that is,-1).
getting process information from the snapshot list requires the use of the Process32First and Process32Next functions, and each call to the function returns information for only one process. The Process32First function is used for the first call, and subsequent calls are made by the
the Process32Next function loops until all the information has been retrieved. The function returns FALSE when there is no longer any remaining information, so the following loop structure is used in the program to get the list of processes.
BOOL bmore =::P Rocess32first (Hprocesssnap, &pe32);
While (bmore)
{//Here process information returned to PROCESSENTRY32
bmore =::P rocess32next (Hprocesssnap, &pe32);
}

Operating effect:

To terminate a process:

Terminating the process is to end the execution of the program and let it unload from memory. There are 4 possible reasons for a process termination:
(1) The entry function of the main thread is returned.
(2) A thread in the process called the ExitProcess function, which can only terminate the current process.
(3) All threads in this process are finished.
(4) A thread in another process called the TerminateProcess function.

To terminate the current process: void // the parameter uexitcode the exit code for this program.  terminating other processes: BOOL terminateprocess (    // handle to end process (target process)    UINT uexitcode    Specify the exit code of the target process, you can use GetExitCodeProcess to get a process exit code );

Before you operate on a process, you must first obtain the process handle for the process. The CreateProcess function will return a process handle after the process is created, and for a process that already exists, you can only use the OpenProcess function to get the process
Access rights, the function usage is as follows:

HANDLE OpenProcess (
DWORD dwdesiredaccess,//desired access rights, can be process_all_access, etc.
BOOL bInheritHandle,//Specifies whether the returned handle can be inherited
DWORD DWPROCESSID//Specifies the ID number of the process to open
);

In general, use the following code to terminate a process:
BOOL Terminateprocessfromid (DWORD dwId)
{
BOOL bRet = FALSE;
Open target process, get process handle
HANDLE hprocess =:: OpenProcess (Process_all_access, FALSE, dwId);
if (hprocess! = NULL)
{//Terminate process

BRet =:: TerminateProcess (hprocess, 0);
}
CloseHandle (hprocess);
return bRet;
}

Win32 process creation, process snapshot, process termination use case

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.