zoukankan      html  css  js  c++  java
  • Windows 获取进程ID

    DWORD GetProcessID(const char *ProcessName)
    {
        PROCESSENTRY32 pe32;
    
        pe32.dwSize = sizeof(PROCESSENTRY32);
    
        //获取进程快照
        HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hProcessSnap == INVALID_HANDLE_VALUE)
        {
            printf("CreateToolhelp32Snapshot 调用失败.
    ");
            return -1;
        }
    
        //遍历进程快照,轮流显示每个进程的信息
        BOOL bMore = ::Process32First(hProcessSnap, &pe32);
        while (bMore)
        {
            printf("进程名称:%ls
    ", pe32.szExeFile); //这里得到的应该是宽字符,用%ls,不然无法正常打印
            printf("进程ID:%u
    
    ", pe32.th32ProcessID);
            if (lstrcmp(pe32.szExeFile, ProcessName) == 0)
            {
                break;
            }
    
            bMore = ::Process32Next(hProcessSnap, &pe32);
        }
    
        //不要忘记清除掉snapshot对象
        ::CloseHandle(hProcessSnap);
    
        return pe32.th32ProcessID;
    }
    
    int main()
    {
        DWORD ID;
        const char *name = "sscom32.exe";
    
        ID = GetProcessID(name);
    
        printf("GetProcessId %s %u
    ", name, ID);
    
        while (1);
    
        return 0;
    }

    需要配置
    1
    或者

    DWORD GetProcessID(LPCWSTR ProcessName)
    {
        PROCESSENTRY32 pe32;
    
        pe32.dwSize = sizeof(PROCESSENTRY32);
    
        //获取进程快照
        HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hProcessSnap == INVALID_HANDLE_VALUE)
        {
            printf("CreateToolhelp32Snapshot 调用失败.
    ");
            return -1;
        }
    
        //遍历进程快照,轮流显示每个进程的信息
        BOOL bMore = ::Process32First(hProcessSnap, &pe32);
        while (bMore)
        {
            //printf("进程名称:%ls
    ", pe32.szExeFile); //这里得到的应该是宽字符,用%ls,不然无法正常打印
            //printf("进程ID:%u
    
    ", pe32.th32ProcessID);
            if (lstrcmp(pe32.szExeFile, ProcessName) == 0)
            {
                break;
            }
    
            bMore = ::Process32Next(hProcessSnap, &pe32);
        }
    
        //不要忘记清除掉snapshot对象
        ::CloseHandle(hProcessSnap);
    
        return pe32.th32ProcessID;
    }
    
    int main()
    {
        DWORD ID;
        LPCWSTR name = _T("sscom32.exe");
    
        ID = GetProcessID(name);
    
        printf("GetProcessId %ls %u
    ", name, ID);
    
        while (1);
    
        return 0;
    }
  • 相关阅读:
    js基础之弹性运动(四)
    js基础之动画(三)
    js基础之动画(二)
    js基础之动画(一)
    js基础之ajax
    js基础之COOKIE
    4.27日软件开发日记我今天做了什么
    4.25日
    4.24日自学成果
    错排公式及其化简
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709366.html
Copyright © 2011-2022 走看看