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

J

joioy

Unregistered / Unconfirmed
GUEST, unregistred user!
如果我知道了Handle,那哪个API能得出该文件的文件名呢?200分
 
看看关于进程 的API吧,没试过。
 
function GetModuleName(Module: HMODULE): string;
var
ModName: array[0..MAX_PATH] of Char;
begin
SetString(Result, ModName, GetModuleFileName(Module, ModName, SizeOf(ModName)));
end;
 
可能是指所打开文件的句柄,不一定是可执行文件。
 
试试getwindowtext(HWND hwnd,LPTSTR lpstring,int nMaxCount)
示例:
hCurrentwindow:=getwindow(Handle,GW_HWNDFIRST);
while hcurrentwindow<>0do
begin
if GetWindowText(hCurrentWindow,@szText,255)>0 then
memo1.lines.add(strpas(@szText));
hCurrentwindow:=getwindow(Handle,GW_HWNDNEXT);
end;
 
文件句柄更简单
F: file;
TFileRec(F).Name 就是文件名
 
我查找过此类的API函数,很可惜,最终也没有找到
 
为何要开两个问题,其实点下面的“将此问题提前”就可以了。
来自:zw84611, 时间:2002-11-27 18:29:00, ID:1465538 | 编辑
---------------------------------------------------------------
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;
 
收贴吧!!
 
有个API函数可以根据对象句柄获得对象的信息数据
我只在获取字体信息时用过,不知道能不能用于文件
Windows下有句柄的东西可能都是对象
GetObjectData
 
顶部