Get Process ID by Name
2008-03-26 09:33
721 查看
DWORD CChatMessageDlg::GetProcessId(LPCTSTR pszProcessName)
...{
BOOL bFound = FALSE;
DWORD aProcesses [1024], cbNeeded, cProcesses;
unsigned int i;
// Enumerate all processes
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return FALSE;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
TCHAR szEXEName[MAX_PATH] = ...{0};
// Loop through all process to find the one that matches
// the one we are looking for
for (i = 0; i < cProcesses; i++)
...{
// Get a handle to the process
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, aProcesses[i]);
// Get the process name
if (NULL != hProcess)
...{
HMODULE hMod;
DWORD cbNeeded;
if(EnumProcessModules(hProcess, &hMod,
sizeof(hMod), &cbNeeded))
...{
//Get the name of the exe file
GetModuleBaseName(hProcess, hMod, szEXEName,
sizeof(szEXEName)/sizeof(TCHAR));
if (_tcsicmp(szEXEName, pszProcessName) == 0)
...{
bFound = TRUE;
CloseHandle(hProcess);
break;
}
}
CloseHandle(hProcess);
}
}
return bFound ? aProcesses[i] : 0;
}
Note:
Header | Declared in Psapi.h. |
---|---|
Library | Link to Psapi.lib. |
相关文章推荐
- GetProcessIDByName
- GetProcessIDByName
- centos7.2 get pid by process name with python3.6
- document.getElementsByName("IPInput3").disabled=(id!=1); 操作无效的原因是应该为
- JavaScript: Get Elements by ID, Tag, Name, Class
- 在Window2003上执行System.Diagnostics.Process.GetProcessesByName等方法失败的原因
- SelectAllStu GetNameById queryStuById
- getElementById Vs getElementsByName(Javascript)细说HTML元素的ID和Name属性的区别
- 由hwnd得到进程名 :GetProcessNameByHandle(HWND nlHandle)
- getElementById Vs getElementsByName(Javascript)细说HTML元素的ID和Name属性的区别
- getElementById Vs getElementsByName(Javascript)细说HTML元素的ID和Name属性的区别
- 以不同用户身份运行程序,/savecred只需要输入一次密码(GetTokenByName取得EXPLORER.EXE的令牌,然后调用CreateProcessAsUser,而且使用LoadUserProfile解决另存文件的问题)good
- js中document.getElementById(ID)与document.getElementsByName(Name)的区别
- getElementById Vs getElementsByName(Javascript)细说HTML元素的ID和Name属性的区别
- getElementById Vs getElementsByName(Javascript)细说HTML元素的ID和Name属性的区别
- getElementById Vs getElementsByName(Javascript)细说HTML元素的ID和Name属性的区别
- div不管有没有name属性,只要设置id和name为同一个值即可实现getElementsByName功能
- Process.GetProcessesByName ,返回一个Process[],这句话是判断当前程序启动了几个
- 在Window2003上执行System.Diagnostics.Process.GetProcessesByName等方法失败的原因
- centos7.2 get pid by process name with python3.6