windows进程函数试炼
实践一下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 }
煮酒论英雄