Einzelnen Beitrag anzeigen

JnZn558

Registriert seit: 22. Aug 2004
98 Beiträge
 
#1

dll dynamisch laden problem

  Alt 16. Dez 2010, 08:46
Delphi-Quellcode:
//dll
library ZDllManager;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }


uses
  Windows,
  SysUtils,
  Classes,
  ufrmDllManager in 'ufrmDllManager.pas{frmPlgMgr};

{$R *.res}

  (* declaration *)
  procedure DllMain( dwReason: DWORD ); forward;
  procedure InitPlgMgr( pParent: Pointer; szTitle: PChar ); stdcall; forward;
  procedure DestroyPlgMgr; stdcall; forward;
  procedure ShowPlgMgr; stdcall; forward;

  (* definition *)
  procedure DllMain( dwReason: DWORD );
  begin
    case dwReason of
      DLL_PROCESS_ATTACH:
      DLL_THREAD_ATTACH:
      DLL_THREAD_DETACH:
      DLL_PROCESS_DETACH: DestroyPlgMgr;
    end;
  end;

  procedure InitPlgMgr( pParent: Pointer; szTitle: PChar ); stdcall;
  begin
    Form1 := TForm1.Create( nil );
    if Assigned( pParent ) then
      frmPlgMgr.Parent := pParent;
    frmPlgMgr.Caption := string( szTitle );
  end;

  procedure DestroyPlgMgr; stdcall;
  begin
    if Assigned( Form1 ) then
      FreeAndNil( Form1 );
  end;

  procedure ShowPlgMgr; stdcall;
  begin
    Form1.Show;
  end;

  exports
    InitPlgMgr,
    ShowPlgMgr;
  (* export function *)
  
begin
  DllProc := @DllMain;
  DllMain( DLL_PROCESS_ATTACH );
end.



//frmdllmanager
unit ufrmDllManager;

interface

uses
  Classes, Controls, Forms,
  Dialogs, ComCtrls;

type
  TForm1 = class(TForm)
    lvPlugins: TListView;
    procedure lvPluginsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.lvPluginsClick(Sender: TObject);
begin
  ShowMessage('hello');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.


// main exe
unit ufrmStart;

interface

uses
  Windows, Dialogs, Classes, Controls, Forms, StdCtrls;

type
  TForm1 = class(TForm)
    btnClose: TButton;
    btnLoad: TButton;
    btnNew: TButton;
    procedure btnCloseClick(Sender: TObject);
    procedure btnNewClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  hPlgMgr : THandle;
  pfrmPlgMgr : procedure( pParent: Pointer; szTitle: PChar ); stdcall;

procedure TForm1.btnCloseClick(Sender: TObject);
begin
  FreeLibrary( hPlgMgr );
  //Close;
end;

procedure TForm1.btnNewClick(Sender: TObject);
begin
  if hPlgMgr = 0 then
  begin
    hPlgMgr := LoadLibrary( 'dll\DllManager.dll' );
    if hPlgMgr = 0 then
      ShowMessage( 'DllManager.dll could not loaded' )
    else
    begin
      pfrmPlgMgr := GetProcAddress( hPlgMgr, 'InitPlgMgr' );
      if Assigned( pfrmPlgMgr ) then
      begin
        pfrmPlgMgr( nil, 'test' );
      end;
    end;
  end;
end;

end.
bei main exe bekomme ich eine exception wenn dll geladen ist, InitPlgMgr aufgerufen, und dann freelibrary aufruft, diese exception tritt nicht bei aufruf der freelibrary funktion auf, sondern nach prozedurende oder beim programm ende
Peace on the world
  Mit Zitat antworten Zitat