Practice the Windows process-related functions:
The code is as follows:
1 //test__getinformation.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <windows.h>6#include <TlHelp32.h>7 8 #ifndef CONST9 #defineConst constTen #endif One A #ifndef in - #defineInch - #endif the - #ifndef out - #defineOut - #endif + - #ifndef INOUT + #defineINOUT A #endif at - #ifndef OPTIONAL - #defineOPTIONAL - #endif - - in namespacetest__getinformation - { to + //gets the current process ID - DWORD GetCurrentProcessId () the { * return:: GetCurrentProcessId (); $ }Panax Notoginseng - //gets the current name the BOOL getcurrentprocessname (INOUT LPTSTR szcurrentprocessname, in DWORD cchcurrentprocessnamesize) + { A if(0!=:: GetModuleFileName (NULL, Szcurrentprocessname, cchcurrentprocessnamesize)) the { + returnTRUE; - } $ returnFALSE; $ } - - //get the process ID based on the process name (you can get the process handle by using the OpenProcess function based on the process ID, and you can do anything with a handle) the BOOL getprocessidbyname (in LPTSTR szprocessname, in DWORD cchprocessnamesize, out Lpdword lpdwprocessid) - {WuyiHANDLE hSnapShot =NULL; the PROCESSENTRY32 stprocessinfor; - WuhSnapShot =:: CreateToolhelp32Snapshot (Th32cs_snapprocess,0); - if(Invalid_handle_value = = hSnapShot)returnFALSE; AboutStprocessinfor.dwsize =sizeof(PROCESSENTRY32); $ if(FALSE = =::P Rocess32first (hSnapShot, &stprocessinfor))returnFALSE; - if(0==:: lstrcmp (szProcessName, stprocessinfor.szexefile)) - { -*lpdwprocessid =Stprocessinfor.th32processid; A returnTRUE; + } the while(TRUE = =::P rocess32next (hSnapShot, &stprocessinfor)) - { $ if(0==:: lstrcmp (szProcessName, stprocessinfor.szexefile)) the { the*lpdwprocessid =Stprocessinfor.th32processid; the returnTRUE; the } - } in returnFALSE; the } the About //get process handle based on process ID the HANDLE Getprocesshandlebyid (in DWORD Dwprocessid, in DWORD dwdesiredaccess) the { the return:: OpenProcess (dwDesiredAccess, TRUE, dwprocessid); + } - the }Bayi int_tmain (intARGC, _tchar*argv[]) the { the //gets the current process ID -DWORD Dwcurrentprocessid =Test__getinformation::getcurrentprocessid (); - the //gets the current name theLPTSTR szcurrentprocessname = (LPTSTR):: HeapAlloc (:: GetProcessHeap (), Heap_zero_memory, max_path*sizeof(TCHAR)); the if(NULL = = szcurrentprocessname)return 0; theBOOL bsuccess =test__getinformation::getcurrentprocessname (Szcurrentprocessname, MAX_PATH); -:: HeapFree (:: GetProcessHeap (),0, szcurrentprocessname); theSzcurrentprocessname =NULL; the the //get the process ID based on the process name (you can get the process handle by using the OpenProcess function based on the process ID, and you can do anything with a handle)94LPTSTR szgocomprocessname = (LPTSTR):: HeapAlloc (:: GetProcessHeap (), Heap_zero_memory, max_path*sizeof(TCHAR)); theLPTSTR szgocomfilename = TEXT ("GoCom.exe"); theDWORD Dwgogcomprocessid =0; the:: wsprintf (Szgocomprocessname, TEXT ("%s"), szgocomfilename);98bsuccess = Test__getinformation::getprocessidbyname (Szgocomprocessname, MAX_PATH, &dwgogcomprocessid); About:: HeapFree (:: GetProcessHeap (),0, szgocomprocessname); -Szgocomprocessname =NULL;101 102 //get process handle based on process ID103HANDLE Hprocesshandle =Test__getinformation::getprocesshandlebyid (Dwgogcomprocessid, process_all_access);104 if(FALSE = = Hprocesshandle)return 0; the :: CloseHandle (Hprocesshandle);106 107 return 0;108}
Windows process function trials