1,183
社区成员
发帖
与我相关
我的任务
分享
function Process(AFileName: string): string;
var
H: THandle;
P, cbNeeded: DWORD;
fileName: string;
iLen: integer;
hMod: HMODULE;
begin
H := FindWindow(nil, pchar(AFileName));
if H <> 0 then
begin
GetWindowThreadProcessId(H, @P);
if P <> 0 then
begin
H := OpenProcess(PROCESS_ALL_ACCESS, false, p);
if H > 0 then
begin
if EnumProcessModules(H, @hMod, sizeof(hMod), cbNeeded) then//就是这里,自己测试的EXE可以获得句柄,在另一个EXE上无法获得。
begin
SetLength(fileName, MAX_PATH);
iLen := GetModuleFileNameEx(h, hMod, PCHAR(fileName), MAX_PATH);
if iLen <> 0 then
begin
SetLength(fileName, StrLen(PCHAR(fileName)));
Result := fileName;
end;
end;
CloseHandle(h);
end;
end;
end;
function TProcessManage.CheckProcess(ProcessName: string;out ProcessID:DWORD): Boolean;
var
aps:array[1..1024] of DWORD;
hProcess, cb:cardinal;
procCnt, hModule:DWORD;
i:integer;
ret:LongBool;
moduleFileName:array[1..100] of ansichar;
begin
Result := False;
EnumProcesses( @aps, sizeof( aps ), cb );
procCnt := cb div sizeof( DWORD );
for i:=1 to procCnt do
begin
hProcess := OpenProcess( PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, aps[i] );
if hProcess = 0 then continue;
ret := EnumProcessModules( hProcess, @hModule, sizeof(hModule), cb );
if not ret then continue;
GetModuleBaseName( hProcess, hModule, @moduleFileName, sizeof( moduleFileName ) );
if lstrcmpi( @moduleFileName, PAnsiChar(ProcessName)) = 0 then
begin
//MessageBox( 0, '已经运行', 'xxx', MB_OK );
ProcessID := aps[i];
Result := True;
end;
end;
end;
请参考。。。。