一个CSDN上的例子程序的问题(列举出所有正在运行的进程),我把分都问光了,大狭们千万不要不理我啊!!
JoriS 2003-10-15 07:23:17 首先说一下我的理解,一个正在运行的进程应该有很多模块和他项关联,比如一个窗口程序,他GDI.DLL一定是和他关联的。所以,一个进程可以找到产生他的模块,而不是系统的DLL,通过下面这个函数,可以找到一个进程中,执行进程的模块。
这个函数是MSDN上的一个演示程序,可是我在自己的机器上运行的时候出现了问题(在2000和XP上分别
测试过,没有在98上测试过)
请大家帮忙看看这个函数,不是很长,后面我都加了注释,拜托一定要看看,。
BOOL GetProcessModule (DWORD dwPID, DWORD dwModuleID,
LPMODULEENTRY32 lpMe32, DWORD cbMe32) //这个函数用来得到一个进程的MODULE,第一个参数是进程ID
//第二个是从PROCESSENGRY32传递过来的dwModule,关于PROCESSENTRY32大家应该很熟悉。第三个参数是传递
//过来的MODULEENTRY32指针,后面是这个结构的大小
{
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
//用GetLastError()发现错误是18-----没有更多的文件。问题的关键就是这里了!
// Do not forget to clean up the snapshot object.
CloseHandle (hModuleSnap);
return (bRet);
}
下面是调用这个函数的函数-------------->>>>>>
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
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));
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( "\nPriority Class Base\t%d\n",
pe32.pcPriClassBase);
printf( "PID\t\t\t%d\n", pe32.th32ProcessID);
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);
}