• 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:

  • 相关阅读:
    MongoDB Driver快速浏览
    字符串方法
    内置函数-sorted()和reversed()
    列表相关
    内置函数-eval()
    内置函数-zip
    内置函数-map
    列表
    内置函数-enumerate
    选择结构与循环结构
  • 原文地址:https://www.cnblogs.com/predator-wang/p/4788728.html
Copyright © 2020-2023  润新知