Use CreateToolhelp32Snapshot , Process32First, and Process32Next to list all processes.
Inside PROCESSENTRY32 you can find the szExeFile member. You can get the process descriptor by calling OpenProcess with the process identifier th32ProcessID inside the same structure.
Once you find the process matching your exe name, you can exit the loop and get the handle.
Note. If you need to list EVERY process no matter what the session is, you must acquire the SE_DEBUG privilege.
At the top of your main call:
acquirePrivilegeByName(SE_DEBUG_NAME);
And here is the definition of acquirePrivilegeByName :
BOOL acquirePrivilegeByName( const TCHAR *szPrivilegeName) { HANDLE htoken; TOKEN_PRIVILEGES tkp; DWORD dwerr; if (szPrivilegeName == NULL) { SetLastError(ERROR_INVALID_PARAMETER); return FALSE; } if (!LookupPrivilegeValue(NULL, szPrivilegeName, &(tkp.Privileges[0].Luid))) return FALSE; tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &htoken)) return FALSE; if (!AdjustTokenPrivileges(htoken, FALSE, &tkp, 0, NULL, NULL) || GetLastError() != ERROR_SUCCESS)
In addition to what I said above, there is an example of how to use the above Win32 API here .
source share