다음은 동적(Static) DLL로 구현 되어져 있다.

  • 정적(Static) DLL 이란? 본래의 DLL이라기 보다는 코드의 큰 함수들을 따로 모듈별로 분리 했다가 실행 시 함께 처리한다.
  • 동적(Dynamic) DLL(런타임 로딩) 이란 ? DLL은 GetProcAddress라는 API를 사용하여 필요에 따라 첨가하고 필요에 따라 해제시킬 수 있다.

DLL 소스 및 정적 DLL 호출 방법은 다음 글을 참고 하기 바란다.

2010/04/27 - [Language/Delphi] - [Delphi] DLL 호출 규칙(Calling Convention) 테스트용 DLL 및 Source

다음 소스는 Delphi 2010에서 제작 되었으나 기본 사용법은 동일하다.


  1: unit UB_Main_DynDLL;
  2: 
  3: interface
  4: 
  5: uses
  6:   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7:   Dialogs, StdCtrls;
  8: 
  9: type
 10:   TfrmUBMainDynDLL = class(TForm)
 11:     btnPascal: TButton;
 12:     edtPascal: TEdit;
 13:     btnCdecl: TButton;
 14:     edtCdecl: TEdit;
 15:     btnStdcall: TButton;
 16:     edtStdcall: TEdit;
 17:     procedure btnPascalClick(Sender: TObject);
 18:     procedure btnCdeclClick(Sender: TObject);
 19:     procedure btnStdcallClick(Sender: TObject);
 20:   private
 21:     { Private declarations }
 22:   public
 23:     { Public declarations }
 24:   end;
 25: 
 26: var
 27:   frmUBMainDynDLL: TfrmUBMainDynDLL;
 28: 
 29: implementation
 30: 
 31: const
 32:   ImportDLLFile = 'UPCallTypeDLLTest.dll';
 33:   Msg = '안녕! 세계 (Hellow World!)';   // Hellow World! 국산화 버전 ㅎㅎ
 34: 
 35: var
 36:   DLLCallTypePascal : function (AStr : string)  : PChar;  pascal;
 37:   DLLCallTypeCdecl  : function (AStr : string)  : PChar;  cdecl;
 38:   DLLCallTypeStdcall: function (AStr : string)  : PChar;  stdcall;
 39: 
 40: {$R *.dfm}
 41: 
 42: procedure TfrmUBMainDynDLL.btnCdeclClick(Sender: TObject);
 43: var
 44:   H : HINST;
 45:   S : PChar;
 46: begin
 47:   H := LoadLibrary(PChar(ImportDLLFile));
 48:   if H <= 0 then
 49:   begin
 50:     ShowMessage('로딩 에러 : ' + IntToStr(GetLastError));
 51:   end else begin
 52:     try
 53:       @DLLCallTypeCdecl := GetProcAddress(H, Pchar('DLLCallTypeCdecl'));
 54:       S := DLLCallTypeCdecl(Msg);
 55:       if H <> 0 then
 56:         edtCdecl.Text := S
 57:       else
 58:         edtCdecl.Text := 'ERROR!!';
 59:     finally
 60:       FreeLibrary(H);
 61:     end;
 62:   end;
 63: end;
 64: 
 65: procedure TfrmUBMainDynDLL.btnPascalClick(Sender: TObject);
 66: var
 67:   H : HINST;
 68:   S : PChar;
 69: begin
 70:   H := LoadLibrary(PChar(ImportDLLFile));
 71:   if H <= 0 then
 72:   begin
 73:     ShowMessage('로딩 에러 : ' + IntToStr(GetLastError));
 74:   end else begin
 75:     try
 76:       @DLLCallTypePascal := GetProcAddress(H, Pchar('DLLCallTypePascal'));
 77:       S := DLLCallTypePascal(Msg);
 78:       if H <> 0 then
 79:         edtPascal.Text := S
 80:       else
 81:         edtPascal.Text := 'ERROR!!';
 82:     finally
 83:       FreeLibrary(H);
 84:     end;
 85:   end;
 86: end;
 87: 
 88: procedure TfrmUBMainDynDLL.btnStdcallClick(Sender: TObject);
 89: var
 90:   H : HINST;
 91:   S : PChar;
 92: begin
 93:   H := LoadLibrary(PChar(ImportDLLFile));
 94:   if H <= 0 then
 95:   begin
 96:     ShowMessage('로딩 에러 : ' + IntToStr(GetLastError));
 97:   end else begin
 98:     try
 99:       @DLLCallTypeStdcall := GetProcAddress(H, Pchar('DLLCallTypeStdcall'));
100:       S := DLLCallTypeStdcall(Msg);
101:       if H <> 0 then
102:         edtStdcall.Text := S
103:       else
104:         edtStdcall.Text := 'ERROR!!';
105:     finally
106:       FreeLibrary(H);
107:     end;
108:   end;
109: end;
110: 
111: end.
112: 





출처 : 자작(http://userpark.net)

+ Recent posts