function InitPSAPI: Boolean;
begin
PSAPIHandle:=GetModuleHandle(PSAPIDLL);
if PSAPIHandle = 0 then
PSAPIHandle:=loadlibrary(psapidll);
if PSAPIHandle<>0 then begin
try
@_EnumProcesses:=getprocaddress(PSAPIHandle,pchar('EnumProcesses'));
@_EnumProcessModules:=getprocaddress(PSAPIHandle,pchar('EnumProcessModules'));
@_GetModuleBaseName:=getprocaddress(PSAPIHandle,pchar('GetModuleBaseNameA'));
@_GetModuleFileNameEx:=getprocaddress(PSAPIHandle,pchar('GetModuleFileNameExA'));
@_GetModuleInformation:=getprocaddress(PSAPIHandle,pchar('GetModuleInformation'));
@_EmptyWorkingSet:=getprocaddress(PSAPIHandle,pchar('EmptyWorkingSet'));
@_QueryWorkingSet:=getprocaddress(PSAPIHandle,pchar('QueryWorkingSet'));
@_InitializeProcessForWsWatch:=getprocaddress(PSAPIHandle,pchar('InitializeProcessForWsWatch'));
@_GetWsChanges:=getprocaddress(PSAPIHandle,pchar('GetWsChanges'));
@_GetMappedFileName:=getprocaddress(PSAPIHandle,pchar('GetMappedFileNameA'));
@_EnumDeviceDrivers:=getprocaddress(PSAPIHandle,pchar('EnumDeviceDrivers'));
@_GetDeviceDriverBaseName:=getprocaddress(PSAPIHandle,pchar('GetDeviceDriverBaseNameA'));
@_GetDeviceDriverFileName:=getprocaddress(PSAPIHandle,pchar('GetDeviceDriverFileNameA'));
@_GetProcessMemoryInfo:=getprocaddress(PSAPIHandle,pchar('GetProcessMemoryInfo'));
except
if not freelibrary(psapihandle) then
raise exception.create('Unload Error: '+psapidll+' ('+inttohex(getmodulehandle(psapidll),8)+')');
end;
end;
result:=(PSAPIHandle<>0) and assigned(_EnumProcesses);
end;
function InitToolHelp: Boolean;
begin
if KernelHandle=0 then begin
KernelHandle:=GetModuleHandle(kernel32);
if KernelHandle<>0 then begin
@_CreateToolhelp32Snapshot := GetProcAddress(KernelHandle, 'CreateToolhelp32Snapshot');
@_Heap32ListFirst := GetProcAddress(KernelHandle, 'Heap32ListFirst');
@_Heap32ListNext := GetProcAddress(KernelHandle, 'Heap32ListNext');
@_Heap32First := GetProcAddress(KernelHandle, 'Heap32First');
@_Heap32Next := GetProcAddress(KernelHandle, 'Heap32Next');
@_Toolhelp32ReadProcessMemory := GetProcAddress(KernelHandle, 'Toolhelp32ReadProcessMemory');
@_Process32First := GetProcAddress(KernelHandle, 'Process32First');
@_Process32Next := GetProcAddress(KernelHandle, 'Process32Next');
@_Process32FirstW := GetProcAddress(KernelHandle, 'Process32FirstW');
@_Process32NextW := GetProcAddress(KernelHandle, 'Process32NextW');
@_Thread32First := GetProcAddress(KernelHandle, 'Thread32First');
@_Thread32Next := GetProcAddress(KernelHandle, 'Thread32Next');
@_Module32First := GetProcAddress(KernelHandle, 'Module32First');
@_Module32Next := GetProcAddress(KernelHandle, 'Module32Next');
@_Module32FirstW := GetProcAddress(KernelHandle, 'Module32FirstW');
@_Module32NextW := GetProcAddress(KernelHandle, 'Module32NextW');
end;
end;
result:=(KernelHandle<>0) and assigned(_CreateToolhelp32Snapshot);
end;
function CreateToolhelp32Snapshot(dwFlags, th32ProcessID: DWORD): THandle;
begin
if InitToolHelp then
Result := _CreateToolhelp32Snapshot(dwFlags, th32ProcessID)
else Result := 0;
end;
function Process32First(hSnapshot: THandle; var lppe: TProcessEntry32): BOOL;
begin
if InitToolHelp then
Result := _Process32First(hSnapshot, lppe)
else Result := False;
end;
function Process32Next(hSnapshot: THandle; var lppe: TProcessEntry32): BOOL;
begin
if InitToolHelp then
Result := _Process32Next(hSnapshot, lppe)
else Result := False;
end;
function GetModuleFileNameEx(hProcess : THandle; module : HInst; FileName : PChar; size : Integer) : Integer; stdcall;
begin
if (psapihandle<>0) and assigned(_GetModuleFileNameEx) then
result:=_GetModuleFileNameEx(hProcess,module,FileName,size)
else
result:=0;
end;
function ProcessFileName(PID: DWORD): string;
var
Handle: THandle;
begin
Result:='';
Handle:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);
if Handle<>0 then
try
SetLength(Result,MAX_PATH);
//if FullPath then begin
if GetModuleFileNameEx(Handle,0,PChar(Result),MAX_PATH)>0 then
SetLength(Result,StrLen(PChar(Result)))
else
Result:='';
//end else begin
// if GetModuleBaseName(Handle,0,PChar(Result),MAX_PATH)>0 then
// SetLength(Result,StrLen(PChar(Result)))
// else
// Result:='';
//end;
finally
CloseHandle(Handle);
end;
end;
//////////////////////////////////////////////////////////////////////
function TForm1.BuildList_ToolHelp32: Boolean;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
FileName: string;
begin
InitPSAPI;
SnapProcHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
Result:=(SnapProcHandle<>INVALID_HANDLE_VALUE);
if Result then
try
ProcEntry.dwSize:=SizeOf(ProcEntry);
NextProc:=Process32First(SnapProcHandle,ProcEntry);
while NextProc do begin
if ProcEntry.th32ProcessID=0 then begin
FileName:='System Idle Process';
end else begin
//if GetOS=os2K then begin
FileName:=ProcessFileName(ProcEntry.th32ProcessID);
if FileName='' then
FileName:=ProcEntry.szExeFile;
//end else begin
// FileName:=ProcEntry.szExeFile;
// if not FullPath then
// FileName:=ExtractFileName(FileName);
//end;
end;
ListBox1.Items.AddObject(FileName,Pointer(ProcEntry.th32ProcessID));
NextProc := Process32Next(SnapProcHandle,ProcEntry);
end;
finally
CloseHandle(SnapProcHandle);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
BuildList_ToolHelp32;
end;
자세한 예제는 제가 예전에 자료실에 올린 소스를 보시면 됩니다.
"음악들을 때 좋은 프로그램(태스크바에서 감추기)" 라고 제목이 되어 있을 겁니다.