您好,我正在尝试使用C语言中的Windows API进行项目开发。我的项目中的一个小部分是获取lsass.exe的进程ID。
我已经尝试了下面的程序,但它无法正常工作。 我已经阅读了有关CreateToolhelp32Snapshot、Process32First和Process32Next函数的文章,可以有人帮我解释如何在代码中使用它们吗?
所以请帮帮我。 我是Windows API的初学者,如果有人能向我推荐一本好的电子书参考,我将不胜感激。
我不知道是否有更简单的方法。这个方法是通过查找每个正在运行的PID,并将其名称与“lsass.exe”进行比较来实现的。
// pid.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <windows.h>
#include <psapi.h>
int PrintProcessNameAndID( DWORD processID, const char *name )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
if(strcmp(szProcessName, name) == 0) // right process
{
CloseHandle(hProcess);
return 1;
}
// Release the handle to the process.
CloseHandle( hProcess );
return 0;
}
int find(const char *name)
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for ( i = 0; i < cProcesses; i++ )
{
if( aProcesses[i] != 0 )
{
if(PrintProcessNameAndID( aProcesses[i], name ))
{
//found it
_tprintf("%d %s\n", aProcesses[i], name);
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
find("lsass.exe");
return 0;
}
这里有一个使用 CreateToolhelp32Snapshot
、Process32First
和 Process32Next
的示例(你需要添加错误处理等,并在代码中包含 tlhelp32.h
)。顺便说一下,这些函数不兼容 Windows NT:
BOOL GetProcessList(const char *processname, DWORD **processIds, int *numprocess)
{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
DWORD *processIdsTmp;
*processIds = NULL;
*numprocess = 0;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( INVALID_HANDLE_VALUE == hProcessSnap ) return( FALSE );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
CloseHandle( hProcessSnap ); // clean the snapshot object
return( FALSE );
}
do
{
if (0 == strcasecmp(processname, pe32.szExeFile))
{
processIdsTmp = realloc(*processIds, sizeof(DWORD) * ((*numprocess) + 1));
if (NULL == processIdsTmp)
{
free(*processIds);
*processIds = NULL;
*numprocess = 0;
CloseHandle( hProcessSnap ); // clean the snapshot object
return( FALSE );
}
*processIds = processIdsTmp;
(*processIds)[(*numprocess)++] = pe32.th32ProcessID;
}
} while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return( TRUE );
}
这里是使用这些函数的完整示例。
这是对 Luis G. Costantini R.
代码的修改。
它使用 MFC:
#include "TlHelp32.h"
BOOL GetProcessList(const TCHAR *processname, CArray<DWORD> &PIDs)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
// Take a snapshot of all processes in the system.
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hProcessSnap) return FALSE;
// Retrieve information about the first process,
// and exit if unsuccessful
if (!::Process32First(hProcessSnap, &pe32))
{
CloseHandle(hProcessSnap); // clean the snapshot object
return FALSE;
}
do
{
if (0 == _tcsicmp(processname, pe32.szExeFile))
{
PIDs.Add(pe32.th32ProcessID);
}
}
while (::Process32Next(hProcessSnap, &pe32));
::CloseHandle(hProcessSnap);
return TRUE;
}