类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
注: win2000系统 , 所以不用 CreateToolSnap
DWORD dwProc[1024], dwLen = 0;
EnumProcesses(dwProc, sizeof(dwProc), &dwLen);
for (DWORD i = 1; i < dwLen / sizeof(DWORD); i++)
{
HANDLE hProc;
TCHAR szProcessName [256] = {0};
HMODULE ahMod[10];
DWORD dwNeeded;
hProc = OpenProcess (PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE,
dwProc[i]);
if (hProc)
{
if (EnumProcessModules (hProc,
ahMod,
sizeof(ahMod),
&dwNeeded))
{
if (GetModuleBaseName(hProc,
ahMod[0],
szProcessName,
sizeof(szProcessName)))
{
TRACE("Proc: %ld :%s\n", dwProc[i], szProcessName);
}
else
{
TRACE("Proc: %ld :UnHandled\n", dwReturn[i]);
}
}
CloseHandle (hProc);
}
}
----------------------------------
上面的语句我检查过了, 不知道哪里错了, 运行时, 出现内存错误0xC0000005: Access Violation
注: 枚举的进程 ID 绝对正确
网友回答:
msdn:
#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 processs 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);
}
#include <windows.h>
#include <stdio.h>
#include "psapi.h"
void PrintModules( DWORD processID )
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Get a list of all the modules in this process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;
if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
{
char szModName[MAX_PATH];
// Get the full path to the modules file.
if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
sizeof(szModName)))
{
// Print the module name and handle value.
printf("\t%s (0x%08X)\n", szModName, hMods[i] );
}
}
}
CloseHandle( hProcess );
}
void main( )
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name of the modules for each process.
for ( i = 0; i < cProcesses; i++ )
PrintModules( aProcesses[i] );
}
我用你的代码编了一个控制台程序,运行完全正常,只是把TRACE改成了printf
#include <windows.h>
#include <psapi.h>
#include <stdio.h>
#pragma comment(lib, "psapi.lib")
int main()
{
DWORD dwProc[1024], dwLen = 0;
EnumProcesses(dwProc, sizeof(dwProc), &dwLen);
for (DWORD i = 1; i < dwLen / sizeof(DWORD); i++)
{
HANDLE hProc;
TCHAR szProcessName [256] = {0};
HMODULE ahMod[10];
DWORD dwNeeded;
hProc = OpenProcess (PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE,
dwProc[i]);
if (hProc)
{
if (EnumProcessModules (hProc,
ahMod,
sizeof(ahMod),
&dwNeeded))
{
if (GetModuleBaseName(hProc,
ahMod[0],
szProcessName,
sizeof(szProcessName)))
{
printf("Proc: %ld :%s\n", dwProc[i], szProcessName);
}
else
{
printf("Proc: %ld :UnHandled\n", dwProc[i]);
}
}
CloseHandle (hProc);
}
}
return 0;
}