[210422] Dll [정적, 동적]

Donghee Lee·2021년 4월 22일
0

Delphi

목록 보기
3/13

Dll 사용 이유
1. 소스코드를 공개하지 않고 특정 모듈을 배포할 때
2. 대형 프로젝트를 단위별로 쪼개서 Dll로 만들어서 모듈단위 테스트와 수정이 용이하게
3. 언어가 다른 것끼지 모듈을 사용할 수 있도록 하기 위해
4. 업데이트 버전을 배포할 때 용이하도록

Dll 파일 호출 오류

function iAdd(n1, n2 : integer): integer; external 'iMath.dll';

이렇게 했는데 호출 못했다고 오류났을 때
''안 경로를 다 적어주면 됨
Dll 함수 호출 시 대소문자 구분

function iAdd(n1, n2 : integer): integer; external 'C:\Users\42021013\Desktop\iMath\iMath.dll';

Dll 정적로드 단점
1. Dll 파일 자체가 누락돼 있으면 메인 프로그램도 아예 실행이 안 됨
2. 메인 프로그램과 같이 로드돼야 하기 때문에 메모리도 많이 차지함


Dll 동적 로드

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private 선언 }
  public
    { Public 선언 }
  end;

var
  Form1: TForm1;

implementation

//**********선언**********
function iAdd(n1, n2 : integer): integer; external 'C:\Users\42021013\Desktop\iMath\iMath.dll';
function iSub(n1, n2 : integer): integer; external 'C:\Users\42021013\Desktop\iMath\iMath.dll';
function iMul(n1, n2 : integer): integer; external 'C:\Users\42021013\Desktop\iMath\iMath.dll';
function iDiv(n1, n2 : integer): integer; external 'C:\Users\42021013\Desktop\iMath\iMath.dll';

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var hModu : integer;
gProc : function(n1, n2:integer):integer;
begin
  try
    hModu := LoadLibrary('C:\Users\42021013\Desktop\iMath\iMath.dll');
    memo1.Lines.Add('Load Library:' + IntToStr(hModu));
    @gProc := GetProcAddress(hModu, 'iAdd');
    Memo1.Lines.Add(IntToStr(gProc(20,30)));
    FreeLibrary(hModu);
  except
    ShowMessage('iMath.dll missing u');
  end;
end;

end.

동적로드 장점

  1. 기본적으로 Dll은 동일하게 만든다.
    ->다만 메인 프로그램에서 Dll에 있는 사용자 함수를 사용하기 위해서는 동적으로 Load 했다가 사용이 끝나면 메모리에서 해지를 해준다

  2. 프로그램 구동시에 Dll 파일이 없어도 오류가 발생하지 않기에 소스코드에서 사용하려는 함수가 들어있는 Dll 파일이 존재하는지 검사해서 파일이 없을 경우에는 화면에 '~~.Dll 파일이 없습니다'라고 경고 메시지를 넣어줌으로써 갑작스레 프로그램이 종료되는 걸 방지할 수 있다.
    -> 필요할 때만 Dll을 Load 하니깐 메모리를 절약할 수 있고 프로그램 초기화면이 뜨는 시간이 빨라지게 된다.

profile
Better than Yesterday

0개의 댓글