zoukankan      html  css  js  c++  java
  • Windows API 之 CreateToolhelp32Snapshot

    CreateToolhelp32Snapshot:

    参考:

    https://msdn.microsoft.com/en-us/library/ms682489%28VS.85%29.aspx

    HANDLE WINAPI CreateToolhelp32Snapshot(
      _In_ DWORD dwFlags,
      _In_ DWORD th32ProcessID
    );
    #include <windows.h>
    #include <TlHelp32.h>
    #include <iostream>
    #include <tchar.h>
    
    using namespace std;
    
    
    DWORD GetProcessIDByName(TCHAR *szProcName)
    {
        DWORD dwProcID = 0;
        HANDLE hProcessSnap;
        PROCESSENTRY32 pe32;
    
        //获取系统中全部进程的快照 Take a snapshot of all processes in the system.
        //TH32CS_SNAPPROCESS:在快照中包含系统中所有的进程。
        hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (INVALID_HANDLE_VALUE == hProcessSnap) 
            return(FALSE);
    
        pe32.dwSize = sizeof(PROCESSENTRY32);
    
        // 获取第一个进程的信息 Retrieve information about the first process,
        // 若获取失败则退出 and exit if unsuccessful
        if (!Process32First(hProcessSnap, &pe32))
        {
            // 清除快照 clean the snapshot object
            CloseHandle(hProcessSnap);
            cout << "!!! Failed to gather information on system processes! 
    " << endl;
            return(NULL);
        }
        //匹配进程ID Matching Process ID
        do
        {
            if (!strcmp(szProcName,pe32.szExeFile))
            {
                //拷贝进程名 Copy Process Name
                cout << pe32.szExeFile << " : " << pe32.th32ProcessID  << endl;
                dwProcID = pe32.th32ProcessID;
            }
            //cout << pe32.szExeFile << endl;
        } while (Process32Next(hProcessSnap, &pe32));
    
        // 清除快照 clean the snapshot object
        CloseHandle(hProcessSnap);
    
        return dwProcID;
    }
    
    int main()
    {
        TCHAR *szProcessName = "chrome.exe";
        GetProcessIDByName(szProcessName);
    
        system("pause");
        return 0;
    }

    找到对应的进程ID:

  • 相关阅读:
    堆排序
    伽马分布
    隔壁-贪心
    对刚—约瑟夫环
    站军姿-两圆并集
    单纯的线性筛素数
    3兔子
    2.圆桌游戏
    1.花
    历史
  • 原文地址:https://www.cnblogs.com/predator-wang/p/4788728.html
Copyright © 2011-2022 走看看