WinAPI: GetModuleFileName、GetModuleHandle

1256 ワード

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  buf: array[0..MAX_PATH] of Char;
  hinst: HMODULE;
begin
  { }
  hinst := GetModuleHandle('gdi32.dll');

  { }
  GetModuleFileName(hinst, buf, Length(buf));
  ShowMessage(buf); {C:\WINDOWS\system32\GDI32.dll}
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  buf: array[0..MAX_PATH] of Char;
begin
  {  GetModuleFileName   0   HInstance  ,  }
  GetModuleFileName(0, buf, Length(buf));
  ShowMessage(buf);

  GetModuleFileName(HInstance, buf, Length(buf));
  ShowMessage(buf);

  { }
  ShowMessage(Application.ExeName);
  ShowMessage(ParamStr(0));
end;

end.