#pragma hdrstop
#include Unith
//
#pragma package(smart_init)
#pragma resource *dfm
TForm *Form;
//定义变量
HANDLE process[];
PROCESSENTRY p;
DWORD process_ids[];
DWORD num_processes;
TCHAR file_name[MAX_PATH];
TCHAR class_name[MAX_PATH];
unsigned i;
//
//初始化TOOLHELP
BOOL InitToolHelp()
{
//动态调用
HINSTANCE DLLinst=LoadLibrary(KERNELDLL);
if(DLLinst)
{
//取各函数在KERNEL中的地址
CreateToolhelpSnapshot=(HANDLE(WINAPI *)(DWORD dwFlagsDWORD thPD))
GetProcAddress(DLLinstCreateToolhelpSnapshot);
ProcessFirst=(BOOL(WINAPI *)(HANDLE hSnapshotLPPROCESSENTRY pe))
GetProcAddress(DLLinstProcessFirst);
ProcessNext=(BOOL(WINAPI *)(HANDLE hSnapshotLPPROCESSENTRY pe))
GetProcAddress(DLLinstProcessNext);
if((!(UINT)CreateToolhelpSnapshot)||(!(UINT)ProcessFirst)||(!(UINT)ProcessNext))
return FALSE;
else
return TRUE;
}
return FALSE;
}
//初始化PSAPI
BOOL InitPSAPI()
{
HINSTANCE PSAPI=LoadLibrary(PSAPIDLL);
if(NULL==PSAPI)
return FALSE;
EnumProcesses=(BOOL(WINAPI *)(DWORD* lpidProcessDWORD cbDWORD *cbNeeded))
GetProcAddress(PSAPIEnumProcesses);
GetModuleFileNameExA=(DWORD(WINAPI *)(HANDLE hProcessHMODULE hModuleLPTSTR lpstrFileNameDWORD nSize))
GetProcAddress(PSAPIGetModuleFileNameExA);
if(NULL == EnumProcesses||NULL == GetModuleFileName)
return FALSE;
else
return TRUE;
}
__fastcall TForm::TForm(TComponent* Owner)
: TForm(Owner)
{
}
//
void __fastcall TForm::FindAllProcessFileNameClick(TObject *Sender)
{
OSVERSIONINFO osinfo;
osinfodwOSVersionInfoSize=sizeof(OSVERSIONINFO);
//取当前操作系统类型
if(GetVersionEx(&osinfo))
{
switch(osinfodwPlatformId)
{
//当前操作系统是WINX
case VER_PLATFORM_WIN_WINDOWS:
if(InitToolHelp())
{
ListBox>Clear();
pdwSize=sizeof(PROCESSENTRY);
//初始化TOOLHELP快照
HANDLE pName=CreateToolhelpSnapshot(THCS_SNAPPROCESSNULL);
//开始查找
BOOL Next=ProcessFirst(pName&p);
i=;
//遍历进程
while(Next)
{
//显示进程
ListBox>Items>Add(pszExeFile);
//根据进程ID获取句并
process[i]=OpenProcess(PROCESS_TERMINATEpthProcessID);
//继续查找
Next=ProcessNext(pName&p);
i++;
}
CloseHandle(pName);
}
break;
//当前操作系统是NT
case VER_PLATFORM_WIN_NT:
if(InitPSAPI())
{
ListBox>Clear();
//获取当前进程个数
EnumProcesses(process_idssizeof(process_ids)&num_processes);
//遍历进程
for(i=; i{ //根据进程ID获取句并
process[i]=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ
process_ids[i]);
//通过句并获取进程文件名
if(GetModuleFileNameExA(process[i]NULLfile_namesizeof(file_name)))
ListBox>Items>Add(file_name);
}
}
break;
}
}
}
//
void __fastcall TForm::ListBoxClick(TObject *Sender)
{
int iCount;
iCount=ListBox>ItemIndex;
ListBox>Hint=ListBox>Items>Strings[iCount];
}
//
else ShowMessage(初始化TOOLHELP失败);
}