windows進程函數試煉

來源:互聯網
上載者:User

標籤:

實踐一下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進程函數試煉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.