windows 进程pid

复制代码
#include <Tlhelp32.h>
#include <atlstr.h>
/***************************GetSpecifiedPID*******************************
功能描述:获取指定进程名的进程ID
输入参数:pszProcessName:进程名
输出参数:dwPID:进程ID
返 回 值:TRUE:查找到对应的进程名,获取进程ID成功;FALSE:获取进程ID失败
**************************************************************************/
BOOL  GetSpecifiedPID(DWORD *dwPID,const TCHAR *pszProcessName)
{
    TCHAR tempInfo[MAX_PATH]={0};
    BOOL  bResult=FALSE;
    PROCESSENTRY32 pe;
    CString  strExeFile,strProcessName;
    strProcessName.Format(_T("%s"),pszProcessName);
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
    Process32First(hSnapshot, &pe); 
    pe.dwSize=sizeof(pe);
    do{  
        strExeFile.Format(_T("%s"),pe.szExeFile);
        if(0==strProcessName.CompareNoCase(strExeFile))
        { 
            *dwPID =pe.th32ProcessID; 
            bResult=TRUE;
            break; 
        }
    }while (Process32Next(hSnapshot, &pe)!=FALSE);
    CloseHandle(hSnapshot); 
    return bResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
 
    DWORD pid;

    GetSpecifiedPID(&pid,_T("notepad.exe"));
    cout<<pid<<endl;
    system("PAUSE");
    return 0;
}
复制代码

 wmic:root\cli>wmic
wmic - 找不到别名。
wmic:root\cli>process

wmic:root\cli>process where caption="notepad.exe" get processid
ProcessId
36248

posted on   lydstory  阅读(175)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-09-24 spring security and shiro
2020-09-24 这是架构吗
2020-09-24 java线程池 任务队列

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示