我知道CSDN藏龙卧虎,各位兄台,能根据进程名得到程序完整路径吗?

解决方案 »

  1.   

    随便写的,有点乱,也不规范,不过希望可以提供一些参考。除了这些方法,你还可以用PDHunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,TLHELP32, StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        FSnapshotHandle : THandle;
      public
        function GetProcessID(ProcessName: string): Dword;
      end;var
      Form1: TForm1;implementation{$R *.dfm}function Tform1.GetProcessID(ProcessName: string): Dword;
    var
      Ret: BOOL;
      s: string;
      FProcessEntry32: TProcessEntry32;
    begin
      Result := 0;
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
      Ret := Process32First(FSnapshotHandle, FProcessEntry32);
      while Ret do
      begin
        s := ExtractFileName(FProcessEntry32.szExeFile);
        if (AnsicompareText(Trim(s),Trim(ProcessName))=0) then
        begin
          Result := FProcessEntry32.th32ProcessID;
          Break;
        end;
        Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
      CloseHandle(FSnapshotHandle);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      PID: integer;
      ModuleListHandle: Thandle;
      ModuleStruct: TMODULEENTRY32;
      Yn: boolean;
    begin
      //假设你要根据delphi的进程名delphi32.exe来得到他的路径全称
      edit1.Text := 'delphi32.exe';
      if Edit1.Text = '' then exit;
      pid := GetProcessID(edit1.Text);
      ModuleListHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pID);
      ModuleStruct.dwSize := sizeof(ModuleStruct);
      yn := Module32First(ModuleListHandle, ModuleStruct);
      if yn then ShowMessage(ModuleStruct.szExePath)
      else ShowMessage('找不到');
      CloseHandle(ModuleListHandle);
    end;
    end.
      

  2.   

    不用那么麻烦,给你,直接paramstr(0)就可以得到了
      

  3.   

    wudi_1982(2007年第一次登陆CSDN,验证码是88822
    非常感谢
    我正好要用到,hehe