Windows Process function trials

Source: Internet
Author: User

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

Related Article

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.