(reprint) Win32 process creation, process snapshot, process termination use case

Source: Internet
Author: User

Win32 process creation, process snapshot, process termination use case
Process creation:
1 #include <windows.h>2 #include <stdio.h>34IntMain ()5{6//Create open system with Notepad process7 Startupinfo SI1 = {sizeof(SI1)};8Process_information Pi1;9char * szCmdLine1 ="Notepad";10if (:: CreateProcess (NULL, SZCMDLINE1, NULL, NULL, FALSE, NULL, NULL, NULL, &AMP;SI1, &PI1))printf ("Create Notepad Process successfully!\n");121314//Create a new CMD process window15char* SzCmdLine2 ="Cmd";Startupinfo Si2 = {sizeof(SI2)};17Process_information Pi2;Si2.dwflags = Startf_useshowwindow;//Specifies that the Wshowwindow member is validSi2.wshowwindow = TRUE;//If this member is set to true, the main window of the new process is displayed, False does not display20+ BOOL BRet =:: CreateProcess (NULL,//Do not specify the file name of the executable hereSzCmdLine2,//Command-line argumentsNULL,//Default process SecurityNULL,//Default Thread SecurityFALSE,//Specifies that the handle within the current process cannot be inherited by the quilt processCreate_new_console,//Creates a new console window for the new process and, if NULL, does not create a new windowNULL,//Environment variables that use this processNULL,//Drives and directories that use this process&SI1,&PI2);3233If(BRet)34{35//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); ("The new process's main thread ID number:%d\n", Pi2.dwthreadid); 0; +}

Operating effect:

Process Snapshot
1 #include <windows.h>2 #include <tlhelp32. H>//Declaring a header file for a snapshot function3 #include <stdio.h>45IntMain ()6{7PROCESSENTRY32 pe32;8 Pe32.dwsize =sizeof(PE32);910//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 processesHANDLE Hprocesssnap =:: CreateToolhelp32Snapshot (Th32cs_snapprocess,0);12if (Hprocesssnap = =INVALID_HANDLE_VALUE)13{printf ("CreateToolhelp32Snapshot call failed! \ n");15Return-1;16}1718//Traverse a process snapshot to display information for each process in turnBOOL bmore =::P Rocess32first (Hprocesssnap, &PE32);20While(bmore)21st{printf ("Process Name:%s\n "23 printf ( " process ID Number:%u\n\n< Span style= "COLOR: #800000" > "24 bmore =::P rocess32next (Hprocesssnap, &pe32); Span style= "COLOR: #008080" >25 }26 27 // Don't forget to erase the snapshot object 28 :: CloseHandle (HPROCESSSNAP); return 0;30}

Comments:
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.

Terminates the current process:// parameter Uexitcode exit code for this program. Terminate other processes: BOOL terminateprocess (////    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;
}

Original address: http://www.cnblogs.com/dongsheng/p/4192228.html

(reprint) 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.