Q&A

  • [DLL] MDI 부모에서 DLL 내의 Child 호출시...
MDI 부모에서 DLL 내의 Child 호출시 정적인 호출은 문제없이 잘 돌아가지만, 동적인 호출은 에러가 생깁니다.

이런 경험을 가지고 계시는 분 있으신지요?



아래는 소스입니다.



---------------------------------------------------------------------------------

[MDI Parent]



unit Unit1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

Menus;



type

TForm1 = class(TForm)

MainMenu1: TMainMenu;

N1: TMenuItem;

N2: TMenuItem;

procedure N2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;



var

Form1: TForm1;



implementation



{$R *.DFM}



procedure TForm1.N2Click(Sender: TObject);

type TProc = procedure(App: TApplication);

var DLLInstance: Thandle;

ShowChild: TProc;

begin

DLLInstance := LoadLibrary('dll.dll');

if (DLLInstance = 0) then

begin

ShowMessage('dll.dll을 메모리로 Load하는데 실패했습니다.');

Exit;

end;



@ShowChild := GetProcAddress(DLLInstance, 'ShowChild');



if (@ShowChild <> nil) then

ShowChild(Application)

else

ShowMessage('DynamicAbout.dll 내의 프로시져 ShowChild의 주소를 얻지 못했습니다.');



FreeLibrary(DLLInstance);

end;



end.

----------------------------------------------------------------------------------



[DLL Main]



library dll;



uses

Windows,

Forms,

SysUtils,

Classes,

dll_unit in 'dll_unit.pas' {Form2};



{$R *.RES}

var DllApp: TApplication;



procedure EndProcess(Reason: Integer);

begin

if (Reason = DLL_PROCESS_DETACH) then

begin

if Assigned(DllApp) then

Application := DllApp;

end;

end;



procedure ShowChild(MdiParent: TApplication);

var Child: TForm2;

begin

if not Assigned(DllApp) then

begin

DllApp := Application;

Application := MdiParent;

end;



Child := TForm2.Create(Application.MainForm);

Child.Show;

end;



exports

ShowChild;



begin

DLLProc := @EndProcess;

end.



----------------------------------------------------------------------------------



[DLL 내의 Child 폼]





unit dll_unit;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls;



type

TForm2 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

procedure FormClose(Sender: TObject; var Action: TCloseAction);

private

{ Private declarations }

public

{ Public declarations }

end;



var

Form2: TForm2;



implementation



{$R *.DFM}



procedure TForm2.Button1Click(Sender: TObject);

begin

Close;

end;



procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);

begin

Action := caFree;

end;



end.



0  COMMENTS