標籤:
實踐一下windows進程相關函數:
代碼如下:
1 // test__getinformation.cpp : 定義控制台應用程式的進入點。 2 // 3 4 #include "stdafx.h" 5 #include <windows.h> 6 #include <TlHelp32.h> 7 8 #ifndef CONST 9 #define CONST const 10 #endif 11 12 #ifndef IN 13 #define IN 14 #endif 15 16 #ifndef OUT 17 #define OUT 18 #endif 19 20 #ifndef INOUT 21 #define INOUT 22 #endif 23 24 #ifndef OPTIONAL 25 #define OPTIONAL 26 #endif 27 28 29 namespace TEST__GETINFORMATION 30 { 31 32 //擷取當前進程id 33 DWORD GetCurrentProcessId() 34 { 35 return ::GetCurrentProcessId(); 36 } 37 38 //擷取當前進行名稱 39 BOOL GetCurrentProcessName(INOUT LPTSTR szCurrentProcessName, IN DWORD cchCurrentProcessNameSize) 40 { 41 if (0 != ::GetModuleFileName(NULL, szCurrentProcessName, cchCurrentProcessNameSize)) 42 { 43 return TRUE; 44 } 45 return FALSE; 46 } 47 48 //根據進程名稱擷取進程id(根據進程id可以用OpenProcess函數擷取進程控制代碼,有了控制代碼就可以做任何事情了) 49 BOOL GetProcessIdByName(IN LPTSTR szProcessName, IN DWORD cchProcessNameSize, OUT LPDWORD lpdwProcessId) 50 { 51 HANDLE hSnapshot = NULL; 52 PROCESSENTRY32 stProcessInfor; 53 54 hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 55 if (INVALID_HANDLE_VALUE == hSnapshot) return FALSE; 56 stProcessInfor.dwSize = sizeof(PROCESSENTRY32); 57 if (FALSE == ::Process32First(hSnapshot, &stProcessInfor)) return FALSE; 58 if (0 == ::lstrcmp(szProcessName, stProcessInfor.szExeFile)) 59 { 60 *lpdwProcessId = stProcessInfor.th32ProcessID; 61 return TRUE; 62 } 63 while (TRUE == ::Process32Next(hSnapshot, &stProcessInfor)) 64 { 65 if (0 == ::lstrcmp(szProcessName, stProcessInfor.szExeFile)) 66 { 67 *lpdwProcessId = stProcessInfor.th32ProcessID; 68 return TRUE; 69 } 70 } 71 return FALSE; 72 } 73 74 //根據進程id擷取進程控制代碼 75 HANDLE GetProcessHandleById(IN DWORD dwProcessId, IN DWORD dwDesiredAccess) 76 { 77 return ::OpenProcess(dwDesiredAccess, TRUE, dwProcessId); 78 } 79 80 } 81 int _tmain(int argc, _TCHAR* argv[]) 82 { 83 //擷取當前進程id 84 DWORD dwCurrentProcessId = TEST__GETINFORMATION::GetCurrentProcessId(); 85 86 //擷取當前進行名稱 87 LPTSTR szCurrentProcessName = (LPTSTR)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH*sizeof(TCHAR)); 88 if (NULL == szCurrentProcessName) return 0; 89 BOOL bSuccess = TEST__GETINFORMATION::GetCurrentProcessName(szCurrentProcessName, MAX_PATH); 90 ::HeapFree(::GetProcessHeap(), 0, szCurrentProcessName); 91 szCurrentProcessName = NULL; 92 93 //根據進程名稱擷取進程id(根據進程id可以用OpenProcess函數擷取進程控制代碼,有了控制代碼就可以做任何事情了) 94 LPTSTR szGoComProcessName = (LPTSTR)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH*sizeof(TCHAR)); 95 LPTSTR szGoComFileName = TEXT("GoCom.exe"); 96 DWORD dwGoGcomProcessId = 0; 97 ::wsprintf(szGoComProcessName, TEXT("%s"), szGoComFileName); 98 bSuccess = TEST__GETINFORMATION::GetProcessIdByName(szGoComProcessName, MAX_PATH, &dwGoGcomProcessId); 99 ::HeapFree(::GetProcessHeap(), 0, szGoComProcessName);100 szGoComProcessName = NULL;101 102 //根據進程id擷取進程控制代碼103 HANDLE hProcessHandle = TEST__GETINFORMATION::GetProcessHandleById(dwGoGcomProcessId, PROCESS_ALL_ACCESS);104 if (FALSE == hProcessHandle) return 0;105 ::CloseHandle(hProcessHandle);106 107 return 0;108 }
windows進程函數試煉