没搞懂你的意思!application.ExeName得到的就是包含这个窗体的可执行文件的路径!如果你的窗体是他调用的DLL或其他的东西的话就有些复杂了,你可以这样做!我的一个函数希望对你有用://得到进程名称和句柄,也可以得到路径 procedure listpro; var lppe: TProcessEntry32; found : boolean; Hand: THandle; s,s1:string; begin Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); found := Process32First(Hand,lppe); while found do begin s:=StrPas(lppe.szExeFile); s1:=inttostr(lppe.th32ProcessID); found := Process32Next(Hand,lppe); form2.ListBox.Items.Add('进程名称:'+s+' 进程ID ::'+S1); end; end;
//借用别人的! hWindow为窗口的句柄function uhfGetWindowModuleFileName(hWindow:THandle): string; type TGetModuleFileNameEx=function(hProcess:THandle;hMod:THandle;szBaseName:PChar;iSize:Integer):Bool;stdcall; TEnumProcessModules=function(hProcess:THandle;var hMod:THandle;cSize:Cardinal;var cSizeOut:Cardinal):Bool;stdcall; var cProcID: DWORD; hSSHandle: THandle; bContinue: Boolean; rProcEntry: TProcessEntry32; hProcHandle: THandle; szName: array[0..1024] of Char; GetModuleFileNameEx: TGetModuleFileNameEx; EnumProcessModules: TEnumProcessModules; hModPSAPI: THandle; hMod: THandle; cDummy: Cardinal; begin result:=''; If Win32Platform = VER_PLATFORM_WIN32_NT then begin hModPSAPI := LoadLibrary('psapi.dll'); if hModPSAPI <> 0 then begin GetModuleFileNameEx := GetProcAddress(hModPSAPI, 'GetModuleFileNameExA'); EnumProcessModules := GetProcAddress(hModPSAPI, 'EnumProcessModules'); if Assigned(GetModuleFileNameEx) and Assigned(EnumProcessModules) then begin if GetWindowThreadProcessID(hWindow, @cProcID) <> 0 then begin hProcHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, cProcID); if hProcHandle <> 0 then begin try if EnumProcessModules(hProcHandle, hMod, sizeof(hMod), cDummy)then if GetModuleFileNameEx(hProcHandle, 0, @szName, sizeof (szName))then Result := StrPas(szName) finally CloseHandle(hProcHandle) end; end; end; end; FreeLibrary(hModPSAPI); end; end else begin if GetWindowThreadProcessID(hWindow, @cProcID) <> 0 then begin hSSHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSSHandle <> INVALID_HANDLE_VALUE) then begin rProcEntry.dwSize := Sizeof(rProcEntry); bContinue := Process32First(hSSHandle, rProcEntry); while bContinue do begin if rProcEntry.th32ProcessID = cProcID then begin Result := rProcEntry.szExeFile; Break; end; bContinue := Process32Next(hSSHandle, rProcEntry); end; CloseHandle(hSSHandle); end; end; end; end;
var buff : array[0..254] of char; dd : string; begin GetModuleFileName(yourHandle,@buff,255); dd := strpas(@buff); showmessage(dd); end;
2.findwindow();
3.给分吧!
procedure listpro;
var lppe: TProcessEntry32;
found : boolean;
Hand: THandle;
s,s1:string;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do begin
s:=StrPas(lppe.szExeFile);
s1:=inttostr(lppe.th32ProcessID);
found := Process32Next(Hand,lppe);
form2.ListBox.Items.Add('进程名称:'+s+' 进程ID ::'+S1);
end;
end;
hWindow为窗口的句柄function uhfGetWindowModuleFileName(hWindow:THandle): string;
type
TGetModuleFileNameEx=function(hProcess:THandle;hMod:THandle;szBaseName:PChar;iSize:Integer):Bool;stdcall;
TEnumProcessModules=function(hProcess:THandle;var hMod:THandle;cSize:Cardinal;var cSizeOut:Cardinal):Bool;stdcall;
var
cProcID: DWORD;
hSSHandle: THandle;
bContinue: Boolean;
rProcEntry: TProcessEntry32;
hProcHandle: THandle;
szName: array[0..1024] of Char;
GetModuleFileNameEx: TGetModuleFileNameEx;
EnumProcessModules: TEnumProcessModules;
hModPSAPI: THandle;
hMod: THandle;
cDummy: Cardinal;
begin
result:='';
If Win32Platform = VER_PLATFORM_WIN32_NT then
begin
hModPSAPI := LoadLibrary('psapi.dll');
if hModPSAPI <> 0 then
begin
GetModuleFileNameEx := GetProcAddress(hModPSAPI, 'GetModuleFileNameExA');
EnumProcessModules := GetProcAddress(hModPSAPI, 'EnumProcessModules');
if Assigned(GetModuleFileNameEx) and Assigned(EnumProcessModules) then
begin
if GetWindowThreadProcessID(hWindow, @cProcID) <> 0 then
begin
hProcHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, cProcID);
if hProcHandle <> 0 then
begin
try
if EnumProcessModules(hProcHandle, hMod, sizeof(hMod), cDummy)then
if GetModuleFileNameEx(hProcHandle, 0, @szName, sizeof (szName))then
Result := StrPas(szName)
finally
CloseHandle(hProcHandle)
end;
end;
end;
end;
FreeLibrary(hModPSAPI);
end;
end
else begin
if GetWindowThreadProcessID(hWindow, @cProcID) <> 0 then
begin
hSSHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSSHandle <> INVALID_HANDLE_VALUE) then
begin
rProcEntry.dwSize := Sizeof(rProcEntry);
bContinue := Process32First(hSSHandle, rProcEntry);
while bContinue do
begin
if rProcEntry.th32ProcessID = cProcID then
begin
Result := rProcEntry.szExeFile;
Break;
end;
bContinue := Process32Next(hSSHandle, rProcEntry);
end;
CloseHandle(hSSHandle);
end;
end;
end;
end;
buff : array[0..254] of char;
dd : string;
begin
GetModuleFileName(yourHandle,@buff,255);
dd := strpas(@buff);
showmessage(dd);
end;