得到指定进程PID

//#include "targetver.h"
#include "stdio.h"
#include <windows.h>
#include <tlhelp32.h>
int GetProcessIdByName(WCHAR* Namestr) //进程名取pid
{
    HANDLE hSnapshot;
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(pe32);//取大小
    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    if (hSnapshot!= NULL) 
    {
        if (Process32First(hSnapshot,&pe32))//取第一个进程
        {
            do {
                if (strcmp((char*)Namestr,(char*)&pe32.szExeFile)==0)//对比
                {
                    return pe32.th32ProcessID;
                }
            } while (Process32Next(hSnapshot,&pe32));//取第下一个进程
        }
        CloseHandle(hSnapshot);//关闭内核对象
    }
    return 0;
}


int main()
{
    int pid=GetProcessIdByName(L"QQ.exe");
    printf("pid=%d
",pid);
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/IMyLife/p/4826269.html