void CDemoDlg::OnButton1() { DWORD aProcesses[1024], cbNeeded, cProcesses; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) //receive all the Process ID Saved in aProcesses {::MessageBox(NULL,TEXT(""),TEXT("Can not EnumProsses"),0);return;} HANDLE hProcess = NULL; //进程句柄 TCHAR szProcessName[MAX_PATH] ={0}; //进程的名称 TCHAR szProcessPath[MAX_PATH] ={0}; //进程路径 HMODULE hMod=NULL; //进程的第一个模块 DWORD ModulecbNeeded; //储存进程模块的大小 cProcesses = cbNeeded / sizeof(DWORD); //the total of Process ID for (int i = 0; i < cProcesses; i++ ) { hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE, aProcesses[i]);//get the handle of every Process which is Indentified by Process ID if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &ModulecbNeeded) ) // 列举某个进程的模块,第一个模块就是创建此进程的主exe程序 { if ( GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) ) //获得程序名 && GetModuleFileNameEx( hProcess, hMod, szProcessPath, sizeof(szProcessPath)) )//获得模块路径 { sprintf(szProcessName+10, " %s",szProcessPath); //与上一句同效,但执行速度欠佳 ::SendMessage(::GetDlgItem(m_hWnd,IDC_LIST1),LB_ADDSTRING,0,(LPARAM)szProcessName); } } CloseHandle(hProcess); } }示例代码:http://cid-3ba16e78a53d2d3d.office.live.com/self.aspx/VC%5EM%5EM/EnumProcess.zip
HANDLE hProcess,
HMODULE hModule,
LPTSTR lpFilename,
DWORD nSize
);
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) //receive all the Process ID Saved in aProcesses
{::MessageBox(NULL,TEXT(""),TEXT("Can not EnumProsses"),0);return;} HANDLE hProcess = NULL; //进程句柄
TCHAR szProcessName[MAX_PATH] ={0}; //进程的名称
TCHAR szProcessPath[MAX_PATH] ={0}; //进程路径
HMODULE hMod=NULL; //进程的第一个模块
DWORD ModulecbNeeded; //储存进程模块的大小 cProcesses = cbNeeded / sizeof(DWORD); //the total of Process ID
for (int i = 0; i < cProcesses; i++ )
{
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE, aProcesses[i]);//get the handle of every Process which is Indentified by Process ID
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &ModulecbNeeded) ) // 列举某个进程的模块,第一个模块就是创建此进程的主exe程序
{
if ( GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) ) //获得程序名
&& GetModuleFileNameEx( hProcess, hMod, szProcessPath, sizeof(szProcessPath)) )//获得模块路径
{
sprintf(szProcessName+10, " %s",szProcessPath); //与上一句同效,但执行速度欠佳
::SendMessage(::GetDlgItem(m_hWnd,IDC_LIST1),LB_ADDSTRING,0,(LPARAM)szProcessName);
}
}
CloseHandle(hProcess);
}
}示例代码:http://cid-3ba16e78a53d2d3d.office.live.com/self.aspx/VC%5EM%5EM/EnumProcess.zip
http://blog.csdn.net/RFEZGC/archive/2009/07/26/4382458.aspx进程路径:你看看每个进程的第一个模块,其实就是进程的信息。GetModuleFileNameEx获取某个进程的第一个模块的路径其实就是进程的路径。
还有能根据进程ID获得进程名称,也知道获得进程的第一个模块就是进程路径,但是有没有哪位高手给指点下,如何实现。
这个路径带着最后得exe,可以使用。path把最后得exe去掉
1>MyFun.obj : error LNK2019: 无法解析的外部符号_GetModuleFileNameExA@16,该符号在函数"public: void __thiscall CMyProgressDlg::OnBnClickedButton1(void)" (?OnBnClickedButton1@CMyProgressDlg@@QAEXXZ) 中被引用
1>MyFun.obj : error LNK2019: 无法解析的外部符号_EnumProcessModules@16,该符号在函数"public: void __thiscall CMyProgressDlg::OnBnClickedButton1(void)" (?OnBnClickedButton1@CMyProgressDlg@@QAEXXZ) 中被引用
1>Debug\MyProgress.exe : fatal error LNK1120: 2 个无法解析的外部命令
请问如何解决?