본문 바로가기
DELPHI(델파이)

[델파이 - DELPHI] DLL 호출바

by Jcoder 2020. 11. 19.

1. 정적 호출 방식

// 호출할 Function or Procedure 선언

function DecryptRecfile(KeyFile, sFilename : string; var ErrMsg : string) : boolean; external 'RecEncryptDLL.dll' name '

Decrypt_File';

 

 

{$R *.dfm}

 

procedure TForm7.Button2Click(Sender : TObject);

begin

  try

    var

      ss : string;

    if DecryptRecfile('C:\test.key', 'C:\Users\JSH\Desktop\새 폴더\암호화.wav', ss) then

      Memo1.Lines.Add(format('Result : %s', [True.ToString]))

    else

      Memo1.Lines.Add(format('Result : %s', [ss]));

  except

    on E : Exception do

      Memo1.Lines.Add(E.Message);

  end;

end;

 

2. 동적 호출 방식

procedure TForm7.Button1Click(Sender : TObject);

var

  hModu : integer;

  test : function(KeyFile, sFilename : string; var ErrMsg : string) : boolean; // 호출할 function 내부 선언

 

begin

  hModu := LoadLibrary('RecEncryptDLL.dll'); // dll 로드 (절대 경로 또는 exe와 함께 있으면 dll 이름만 호출)

  try

    try

      Memo1.Lines.Add(format('handle : %d', [hModu]));

 

      @test := GetprocAddress(hModu, 'Decrypt_File'); // test : function(KeyFile, sFilename : string; var ErrMsg : string) : boolean; 

 

      var

        ss : string;

      if test('C:\test.key', 'C:\Users\JSH\Desktop\새 폴더\암호화.wav', ss) then

        Memo1.Lines.Add(format('Result : %s', [True.ToString]))

      else

        Memo1.Lines.Add(format('Result : %s', [ss]));

    except

      on E : Exception do

        Memo1.Lines.Add(E.Message);

    end;

  finally

    FreeLibrary(hModu);

  end;

end;