如果我知道了window应用程序的Handle,那哪个API能得出该文件的文件名呢?100(100分)

  • 主题发起人 主题发起人 joioy
  • 开始时间 开始时间
J

joioy

Unregistered / Unconfirmed
GUEST, unregistred user!
如果我知道了window应用程序的Handle,那哪个API能得出该文件的文件名呢?
 
参考以下程序,比较 handle


120. 获得进程列表,并终止 Excel 进程
procedure TForm1.Button1Click(Sender: TObject);
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
Ret : BOOL;
ProcessID : integer;
s:string;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
Ret:=Process32First(FSnapshotHandle,FProcessEntry32);
Memo1.clear;
while Ret do
begin
Memo1.lines.add(FProcessEntry32.szExeFile);
s:=ExtractFileName(FProcessEntry32.szExeFile);
if s='EXCEL.EXE' then
begin
ProcessID:=FProcessEntry32.th32ProcessID;
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,ProcessID),1);
s:='';
end;
Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;

 
Delphi的函数GetModuleName就可以

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetModuleName(hInstance));
end;
 
win9x下据说下面的可以,没试过:
procedure TForm1.Button2Click(Sender: TObject);
var
fname: array[0..MAX_PATH]of char;
wnd : integer;
Instance: LongWord;
begin
wnd := FindWindow('IEFrame',nil);
if wnd = 0 then ShowMessage('not find');
Instance := GetWindowLong(Wnd, GWL_HINSTANCE);
GetModuleFileName(Instance,fname,MAX_PATH);
caption := fname;
end;

win2k下:
procedure TForm1.Button2Click(Sender: TObject);
type
integer = DWORD; // different versions of psapi.pas floating around
var
i,j,pidNeeded,modNeeded : Integer;
PIDList : array[0..1000] of Integer; // 1000 should be enough
MODList : array[0..1000] of HInst;
PIDName : array [0..MAX_PATH - 1] of char;
MODName : array [0..MAX_PATH - 1] of char;
PH : THandle;

wnd: integer;
pID: integer;
begin
wnd := FindWindow('IEFrame',nil);
GetWindowThreadProcessId(wnd,@pID);

if not enumprocesses (@PIDList, 1000, pidNeeded) then
begin
ShowMessage('Need psapi.dll');
exit;
end;

for i := 0 to (pidNeeded div sizeof (Integer)- 1) do
begin
PH := OpenProcess (PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,PIDList);
if PH <> 0 then
begin
if GetModuleBaseName (PH, 0, PIDName, sizeof (PIDName)) > 0 then
begin
ListBox1.Items.Add('process : ' + PIDName);
if not EnumProcessModules (PH,@MODList,1000, modNeeded) then modNeeded:= 0;

if pID = PIDList then
begin
//caption := PIDName; //here, get the name
if GetModuleFileNameEx (PH, MODList[0], MODName,sizeof(MODName)) > 0 then
caption := MODName;
end;

if PH > 0 then CloseHandle(PH);
end;
end;
end;

end;
 
呵呵,没有那么复杂喔!

procedure TForm1.Button1Click(Sender: TObject);
var
_procfile : String;
begin
setlength(_procfile,119);
getmodulefilename(
getwindowlong(
form1.handle{可以替换为你的窗口句柄},
GWL_HINSTANCE),
pchar(_procfile),119);

_procfile:=string(pchar(_procfile));

//显示程序路径
showmessage(_procfile);
end;
 
多人接受答案了。
 
后退
顶部