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;
    }
  • 相关阅读:
    python之socket编程
    python之异常处理
    面向对象进阶
    openstack的网络配置
    VLAN,GRE,VXLAN
    三节点搭建openstack-Mitaka版本
    矩阵转换
    判断区域相交的方法
    Centos 6.5 Multipath 初始配置
    CentOS开机自动运行程序的脚本
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709366.html
Copyright © 2011-2022 走看看