[MSDN][列举][进程]MSDN中列举进程代码无法运行出结果问题
09月 18th, 2008 by admin
Posted in VC/MFC | No Comments »
我复制了MSDN中列举所有进程的代码,但执行后无法打印出结果,
不知道是怎么回事:
BOOL GetProcessW1::GetProcessList(){
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
return (FALSE);
// Fill in the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Walk the snapshot of the processes, and for each process,
// display information.
if (Process32First(hProcessSnap, &pe32))
{
DWORD dwPriorityClass;
BOOL bGotModule = FALSE;
MODULEENTRY32 me32 = {0};
do
{
bGotModule = GetProcessModule(pe32.th32ProcessID,
pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));
if (bGotModule)
{
HANDLE hProcess;
// Get the actual priority class.
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, pe32.th32ProcessID);
dwPriorityClass = GetPriorityClass (hProcess);
CloseHandle (hProcess);
// Print the process's information.
printf( "
Priority Class Base %d
",
pe32.pcPriClassBase);
printf( "PID %d
", pe32.th32ProcessID);
printf( "Thread Count %d
", pe32.cntThreads);
printf( "Module Name %s
", me32.szModule);
printf( "Full Path %s
", me32.szExePath);
//
CString str,strTemp=" ";
str.Format("%d",pe32.pcPriClassBase);
strTemp+=str;
strTemp+=" ; ";
str.Format("%d",pe32.th32ProcessID);
strTemp+=str;
strTemp+=" ; ";
str.Format("%d",pe32.cntThreads);
strTemp+=str;
strTemp+=" ; ";
str.Format("%s",me32.szModule);
strTemp+=str;
strTemp+=" ; ";
str.Format("%s",me32.szExePath);
strTemp+=str;
strTemp+=" ; ";
pMainView->m_ProcessList.AddString(strTemp);
pMainView->UpdateData(FALSE);
}
}
while (Process32Next(hProcessSnap, &pe32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of processes
// Do not forget to clean up the snapshot object.
CloseHandle (hProcessSnap);
return (bRet);
}
BOOL GetProcessModule (DWORD dwPID, DWORD dwModuleID,
LPMODULEENTRY32 lpMe32, DWORD cbMe32)
{
BOOL bRet = FALSE;
BOOL bFound = FALSE;
HANDLE hModuleSnap = NULL;
MODULEENTRY32 me32 = {0};
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
if (hModuleSnap == INVALID_HANDLE_VALUE)
return (FALSE);
// Fill the size of the structure before using it.
me32.dwSize = sizeof(MODULEENTRY32);
// Walk the module list of the process, and find the module of
// interest. Then copy the information to the buffer pointed
// to by lpMe32 so that it can be returned to the caller.
if (Module32First(hModuleSnap, &me32))
{
do
{
if (me32.th32ModuleID == dwModuleID)
{
CopyMemory (lpMe32, &me32, cbMe32);
bFound = TRUE;
}
}
while (!bFound && Module32Next(hModuleSnap, &me32));
bRet = bFound; // if this sets bRet to FALSE, dwModuleID
// no longer exists in specified process
}
else
bRet = FALSE; // could not walk module list
// Do not forget to clean up the snapshot object.
CloseHandle (hModuleSnap);
return (bRet);
}
编译之后有没有出错啊??
代码怎么可以全部复制呢?? 你得修改下 适合自己的工程啊 你看看里面的 CString 需要的是 mfc支持的 printf 是控制台的 你建立工程的时候 选择对了没有??必须的头文件包含了没有??
编译是没有问题的,问题也不是出在这里。问题是程序根本没有进入到打印那段语句块里面,也就是说
if (bGotModule) 中的bGotModule始终是为FALSE的,含CString那一段是我自己加入的
me32.dwSize = sizeof(MODULEENTRY32);
??,程序中有这一句呀
单步调试下,看到什么地方运行不下去了?
运行得下去,就是运行GetProcessModule返回FALSE,不知道怎么回事,帮忙调试一下,谢谢
经过试验得出,如果不判断dwModuleID,则可以得到信息,在我的机器上me32.th32ModuleID 一直为1,而传入的dwModuleID为0
// do
{
// if (me32.th32ModuleID == dwModuleID)
{
CopyMemory (lpMe32, &me32, cbMe32);
bFound = TRUE;
}
}
// while (!bFound && Module32Next(hModuleSnap, &me32));
//以前写的,模拟任务管理器的
HANDLE snapshot;
snapshot=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 processliststr[50];
for(int j=0;j <50;j++)
processliststr[j].dwSize=sizeof(PROCESSENTRY32);
BOOL return_value;
int i=0;
int m;
return_value=::Process32First(snapshot,&processliststr[i]);
m_showlist.DeleteAllItems();
CString str;
while(return_value)
{
m=m_showlist.InsertItem(i,(LPTSTR)processliststr[i].szExeFile);
str.Format("%d",processliststr[i].cntThreads);
m_showlist.SetItemText(m,1,str);
str.Format("%d",processliststr[i].th32ProcessID);
m_showlist.SetItemText(m,2,str);
str.Format("%d",processliststr[i].pcPriClassBase);
m_showlist.SetItemText(m,3,str);
m_ProcessIndex[i]=processliststr[i].th32ProcessID;
i++;
return_value=::Process32Next(snapshot,&processliststr[i]);
}
你自已调试下,在GetProcessModule里面step into, 看到哪里不执行返回FALSE?
的确,请问是怎么回事,我把这个判断去掉就OK了,MSDN中的这个判断是什么意思?
me32.dwSize = sizeof(MODULEENTRY32);
你自己再仔细看看。
没搞懂,请指点
th32ModuleID
Module identifier of the process. The contents of this member has meaning only to the tool help functions. It is not a handle, nor is it usable by functions other than the ToolHelp functions.
th32ModuleID
Module identifier in the context of the owning process. The contents of this member has meaning only to the tool help functions. It is not a handle, nor is it usable by functions other than the ToolHelp functions.
看下这两段英文就明白了…仅仅是对函数调用产生影响,用于模块比较是错误的…
是呀,你的这个英文文档哪里面的,我的代码可是在MSDN中的,不是我自己写的,我搞不大明白了
调整权集,将运行级上调到最高