Process to PID

from the Internet
// processtopid.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include
#include


int main(int argc, char* argv[])
{
//遍历所有进程,进行注入
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};
DWORD proPid = 0;
//DWORD dwCurrentId;
//得到本程序的PID
//dwCurrentId = ::GetCurrentProcessId();
//对系统中的所有运行进程进行快照
hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
//获取快照失败
return FALSE;
}
ZeroMemory(&pe32,sizeof(PROCESSENTRY32));
    pe32.dwSize=sizeof(PROCESSENTRY32);

while(Process32Next(hProcessSnap,&pe32))
    {
//printf("\t%s----%d\n",pe32.szExeFile,pe32.th32ProcessID);
//char proNametmp[16]={"svchost.exe"};
        printf("Process name is %s\n",pe32.szExeFile);
        if(!_strnicmp(pe32.szExeFile,pe32.szExeFile+5,1)&&!_strnicmp("t",(pe32.szExeFile+6),1))
        { 
 printf("GOT IT!\n%s PID is%d\n",pe32.szExeFile,pe32.th32ProcessID);
              proPid=pe32.th32ProcessID;
              //break;
        }
    }
    CloseHandle(hProcessSnap);
//printf("%ld",proPid);
return proPid;
}

0 评论: