Delphi DLL动态错误引发了许多连续异常

时间:2013-04-18 12:01:26

标签: delphi dll

我从Delphi示例代码中找到了这个源代码,并且 我在Delphi动态DLL中添加一个控件或组件,我无法弄明白,

library DLLEntryLib;

uses
  SysUtils,
  Windows,
  Dialogs,
  Classes,
  msHTML,
  SHDocVw;

type
TMyWeb = class(TWebBrowser)
constructor create(Aowner: TComponent); override;
end;

var
web: TMyWeb;

// Initialize properties here
constructor TMyWeb.Create(AOwner: TComponent);
begin
inherited Create(Self);
end;

procedure getweb;
begin
    web := TmyWeb.create(nil);
    web.Navigate('http://mywebsite.com');
end;


procedure xDLLEntryPoint(dwReason: DWord);
begin
  case dwReason of
    DLL_PROCESS_ATTACH:
    begin
    getweb; //I THINK THE ERROR IS HERE, HOW TO WORK THIS OUT?
    ShowMessage('Attaching to process');
    end;
    DLL_PROCESS_DETACH: ShowMessage('Detaching from process');
    DLL_THREAD_ATTACH:  MessageBeep(0);
    DLL_THREAD_DETACH:  MessageBeep(0);
  end;
end;

begin
  { First, assign the procedure to the DLLProc variable }
  DllProc := @xDLLEntryPoint;
  { Now invoke the procedure to reflect that the DLL is attaching to the
    process }
  xDLLEntryPoint(DLL_PROCESS_ATTACH);
end.


//IN MY APPLICATION FORM.
procedure TMainForm.btnLoadLibClick(Sender: TObject);
begin
if LibHandle = 0 then
begin
LibHandle := LoadLibrary('DLLENTRYLIB.DLL');
if LibHandle = 0 then
  raise Exception.Create('Unable to Load DLL');
end
else
MessageDlg('Library already loaded', mtWarning, [mbok], 0);
end;

如何摆脱错误? 引起许多疑惑异常

1 个答案:

答案 0 :(得分:1)

当你写:

inherited Create(Self);

你应该写

inherited Create(AOwner);

您要求控件拥有自己。那是行不通的。如果构造函数失败,这很可能会导致非终止递归。

另一个大问题是您在DllMain内创建了一个Web浏览器控件。这是一个非常大的禁忌。你会想要停止这样做。将该代码移动到单独的导出函数中。在DllMain中无所作为。

大概是调用者已经初始化了COM。如果没有,您需要确保呼叫者这样做。如果调用者是VCL表单应用程序,则COM将自动初始化。