msdn中的一个例子,请高手进

coding-cpde 2003-08-20 12:05:42
为啥PROCESSENTRY32 定义的结构中的th32ModuleID的返回值是0?
PROCESSENTRY32 中的th32ModuleID 应该和MODULEENTRY32 中的th32ModuleID 相对应,但是由Process32First函数得到的pe32中的th32ModuleID 是0,所以就对应不上了
请高手指点!

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <iostream.h>

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)
{
printf("find the module");
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);
}

BOOL 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));

cout<<pe32.th32ModuleID<<endl;//怎么返回值是0??

if (!bGotModule)
{
HANDLE hProcess;

// Get the actual priority class.
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, pe32.th32ProcessID);
dwPriorityClass = GetPriorityClass (hProcess);
CloseHandle (hProcess);

if(pe32.th32ProcessID == 156)
printf("find the process!\n");

// Print the process's information.
printf( "\nPriority Class Base\t%d\n",
pe32.pcPriClassBase);
printf( "PID\t\t\t%d\n", pe32.th32ProcessID);
printf("PModul ID\t\t%d\n",pe32.th32ModuleID);
printf( "Thread Count\t\t%d\n", pe32.cntThreads);
printf( "Module Name\t\t%s\n", me32.szModule);
printf( "Full Path\t\t%s\n\n", me32.szExePath);
}
}
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);
}

int main()
{
GetProcessList();
return 0;
}

...全文
20 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fbmsf 2003-08-20
  • 打赏
  • 举报
回复
如果是nt,or 2000 要注意是否有管理员权限。
earthquake 2003-08-20
  • 打赏
  • 举报
回复
我试了一下,没问题啊。返回不是0啊.

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧