////得到创建本窗口的文件名
    //   1   -   获取窗口句柄
    //   2   -   获取进程ID
    GetWindowThreadProcessId(hw, ProcID);
    //   3   -   通过进程ID获取进程句柄
    hProcess := OpenProcess(PROCESS_ALL_ACCESS, True, ProcID);
    if   hProcess <> 0   then
    begin
      //   4   -   获取路径(这个函数在psapi中)
      if GetModuleFileNameEx(hProcess, 0, sBuf, SizeOf(sBuf) ) <> 0 then
        Edit1.text := StrPas(sBuf);
    end;
GetModuleFileNameEx函数在Win98下总是返回0,而WinXP正常,why呀?!!

解决方案 »

  1.   

    GetModuleFileNameEx
    Requirements
    Client: Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0.
      

  2.   

    在win9x下,用 tlhelp32中的:
    CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)/ Process32First/Process32Next可以得到系统所有进程的信息,其中PROCESSENTRY32中的szExeFile就含路径(在win2k以上它不含路径)
      

  3.   

    //测试程序如下,在win9x中p.szExeFile为全路径,而win2k/XP中为文件名
    unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;implementationuses tlhelp32;
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
         p:PROCESSENTRY32;
         h:tHANDLE;
    begin
       Memo1.Clear;
       h:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
       p.dwSize:=sizeof(p);
       Process32First(h,p);
       repeat
            Memo1.Lines.add(format('%.8x  %s',[p.th32ProcessID,p.szExeFile]));
       until  not Process32Next(h,p);
       CloseHandle(h);
    end;end.
    如果你已有进程的ID,只要和p.th32ProcessID比较一下就可
      

  4.   

    确实是通过CreateToolhelp32Snapshot创建进程快照来处理的