用win32 模式编译的程序只能枚举处win7 64位系统进程里面的32的程序
用64位编译可以枚举出win7 64位系统所有进程信息
#include <Windows.h>
typedef BOOL (_stdcall * My_EnumProcessesModules)(HANDLE hProcess, // handle to process
HMODULE *lphModule, // array of module handles
DWORD cb, // size of array
LPDWORD lpcbNeeded ); // number of bytes returned
typedef DWORD (_stdcall * My_GetModuleBaseName)(
HANDLE hProcess, // handle to process
HMODULE hModule, // handle to module
LPSTR lpBaseName, // base name buffer
DWORD nSize ); // maximum characters to retrieve
typedef BOOL (_stdcall * My_EnumProcesses)(
DWORD *lpidProcess, // array of process identifiers
DWORD cb, // size of array
DWORD *cbNeeded ); // number of bytes returned
typedef HANDLE(_stdcall * My_OpenProcess)( DWORD dwDesiredAccess, // access flag
BOOL bInheritHandle, // handle inheritance option
DWORD dwProcessId // process identifier
);
char* GetProcessName(HANDLE hProcess)
{
HMODULE hMod;
HMODULE hModBaseName;
DWORD cbNeededM;
char *szProcessName;
My_EnumProcessesModules fpEnumProcessModulesAddr;
My_GetModuleBaseName fpGetModuleBaseName;
hMod = LoadLibraryA("Psapi.dll");
if (hMod == NULL)
{
return NULL;
}
fpEnumProcessModulesAddr = (My_EnumProcessesModules)GetProcAddress(hMod, "EnumProcessModules");
fpGetModuleBaseName = (My_GetModuleBaseName)GetProcAddress(hMod, "GetModuleBaseNameA");
if((fpGetModuleBaseName == NULL) ||
(fpEnumProcessModulesAddr == NULL))
{
FreeLibrary(hMod);
return NULL;
}
szProcessName = (char *)malloc(MAX_PATH);
if(fpEnumProcessModulesAddr( hProcess, &hModBaseName, sizeof(hModBaseName), &cbNeededM))
{
if(fpGetModuleBaseName(hProcess, hModBaseName, szProcessName, MAX_PATH))
{
FreeLibrary(hMod);
return szProcessName;
}
else
{
FreeLibrary(hMod);
free(szProcessName);
return NULL;
}
}
else
{
free(szProcessName);
return NULL;
}
}
BOOL EnumAllProcess()
{
HMODULE hMod;
HMODULE hMod2;
HANDLE hProcess;
My_EnumProcesses fpMy_EnumProcesses;
My_OpenProcess fpMy_OpenProcess;
DWORD aProcesses[1024], cbNeeded, cProcesses;
hMod = LoadLibraryA("Psapi.dll");
hMod2 = LoadLibraryA("Kernel32.dll");
if ((hMod == NULL) ||
(hMod2 == NULL))
{
return FALSE;
}
fpMy_EnumProcesses = (My_EnumProcesses)GetProcAddress(hMod, "EnumProcesses");
fpMy_OpenProcess = (My_OpenProcess)GetProcAddress(hMod2, "OpenProcess");
if ((fpMy_EnumProcesses == NULL)||
(fpMy_OpenProcess == NULL))
{
FreeLibrary(hMod);
FreeLibrary(hMod2);
return FALSE;
}
if (!fpMy_EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) //遍历进程 cbNeeded为返回了多少byte
{
FreeLibrary(hMod2);
FreeLibrary(hMod);
return FALSE;
}
cProcesses = cbNeeded / sizeof(DWORD);
for (int i = 0; i < cProcesses;i++)
{
hProcess = fpMy_OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, aProcesses[i]);
if (hProcess)
{
char *pName = GetProcessName(hProcess);
if (pName != NULL)
{
printf("%s\n",pName);
free(pName);
pName = NULL;
}
}
}
FreeLibrary(hMod2);
FreeLibrary(hMod);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumAllProcess();
//HANDLE hProcess = GetCurrentProcess();
//char *pName = GetProcessName(hProcess);
//if (pName != NULL)
//{
// free(pName);
// pName = NULL;
//}
//else
//{
// printf("获取进程名失败!");
//}
return 0;
}