Skip to content

Instantly share code, notes, and snippets.

@yufanpi
Last active August 29, 2015 14:02
Show Gist options
  • Save yufanpi/6ae886919dba2b96b9ce to your computer and use it in GitHub Desktop.
Save yufanpi/6ae886919dba2b96b9ce to your computer and use it in GitHub Desktop.
GetProcessID by process name
#include <TlHelp32.h>
BOOL GetProcessID(const wchar_t* wszProcessName, DWORD& dwProcID)
{
HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, dwProcID);
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL bOk = ::Process32First(hSnapShot, &pe);
while (bOk)
{
if (wcsstr(pe.szExeFile, wszProcessName) != NULL)
{
dwProcID = pe.th32ProcessID;
return TRUE;
}
bOk = ::Process32Next(hSnapShot, &pe);
}
::CloseHandle(hSnapShot);
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment