0

我从 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;

如何摆脱错误?引发多次连续异常

4

1 回答 1

1

当你写:

inherited Create(Self);

你应该写

inherited Create(AOwner);

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

另一个大问题是您正在内部创建一个 Web 浏览器控件DllMain。这是一个非常大的禁忌。你会想停止这样做。将该代码移动到单独的导出函数中。中什么都不做DllMain

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

于 2013-04-18T13:47:12.893 回答