报错是在主程序退出时,错误信息如下:
xxx.exe raised too many consecutive exceptions:'Access violation at 0x01193458:read of address 0x00951934'代码如下:
主程序:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
      procedure LoadUIDLL(ProgName:string);
  public
    { Public declarations }
  end;
  TShowUIForm = procedure (AHandle: TApplication;ConStr:String;
                          Userid:String;Lug:string;sc:TScreen;FrmName:string);stdcall;
var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.LoadUIDLL(ProgName:string);
var
 LibHandle: HWND;
 ShowUIForm: TShowUIForm;
begin
  LibHandle := LoadLibrary(Pchar(ProgName+'.dll'));
    if LibHandle < 32 then
      begin
      Messagedlg('test1',mterror,[mbok],0);
      exit;
      end;
    @ShowUIForm := GetProcAddress(LibHandle,'ShowUIForm');
    if @ShowUIForm <> nil then
      try
        ShowUIForm(Application,'13123','13123','1',
                   Screen,'Form1');
      except
        raise Exception.Create('test error');
      end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
LoadUIDLL('Project2');
end;end.
DLL程序:
library Project2;uses
  shareMem,
  Windows,
  Graphics,
  Messages,
  Forms,
  Controls,
  SysUtils,
  Classes,
  Dialogs,
  Unit2 in 'Unit2.pas' {Form1};var
 DLLApp:TApplication;
 DLLScreen:TScreen;
 TheForm:TForm;
 TheClass:TPersistentClass;
{$R *.res}
procedure ShowUIForm(AHandle: TApplication;
                     ConStr:String;
                     Userid:String;
                     Lug:string;
                     sc:TScreen;
                     FrmName:string
                     );stdcall;
begin
   Application := AHandle;
   Screen:=sc;
   RegisterClass(TForm1);
   theclass:=GetClass('T'+FrmName);
   if (theclass<>nil) and theclass.InheritsFrom(TForm) then
     begin
     theForm:=TForm(theclass.Create).Create(nil);
     theForm.ShowModal;
     end;
End;procedure DLLUnloadProc(Reason:Integer);register;
begin
 if Reason = DLL_PROCESS_DETACH THEN
   begin
   Application := DLLApp;
   screen:=DLLScreen;
   FreeAndNil(theForm);
   SendMessage(Application.Handle,WM_CLOSE,0,0);
   FreeLibrary(Application.Handle);
   end;
end;exports
  ShowUIForm;begin
 DLLApp:=Application;
 DLLScreen:=Screen;
 DLLProc:=@DLLUnloadProc;
end.