Question

I want to develop a COM DLL in delphi that will internally create a window or form and then display the TWebBrowser navigation on that. The reason of this is I don't want to use the TWebbrowser control to be drag on each of my client app. This client app simply use this DLL because this DLL will also has some other logic that is not relevent to mention here. Please help me how to achieve this

Was it helpful?

Solution

You should heed the reservations of the other posters, but if you want a dll that launches a TWebBrowser this should get you started. It compiles and runs but has only been very briefly tested.

Hope that helps.

library BrowserDLL;

uses
  ShareMem,
  SysUtils,
  Classes,
  Forms,
  Windows,
  DLLMainForm in 'DLLMainForm.pas' {MainForm};

{$R *.RES}

function ShowBrowserForm(AHandle: THandle; const AURL : String): Longint; stdcall;
begin
  Application.Handle := AHandle;
  result := TMainForm.ShowForm(AURL);
end;


exports
  ShowBrowserForm;

var
  DLLApplication : TApplication;


procedure DLLHandler(Reason: Integer);
begin
  case Reason of
    DLL_PROCESS_DETACH:
       begin
         Application := DLLApplication;
       end;
  end;
end;

begin
  DLLApplication := Application;
  DLLproc:=@DLLHandler;
end.

unit DLLMainForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OleCtrls, SHDocVw, ExtCtrls;

type
  TMainForm = class(TForm) 
    wb1: TWebBrowser;
  private 
    FURL: string;
    procedure SetUrl(const Value: string);
  public
    class function ShowForm(const AURL: String): Longint;
    property URL : string read FURL write SetUrl;
  end;

implementation

{$R *.DFM}

{ TBrowserForm }

procedure TMainForm.SetUrl(const Value: string);
begin
  if FURL <> Value then begin
    FURL := Value;
    wb1.Navigate(Value);
  end; 
end;

class function TMainForm.ShowForm(const AURL : String): Longint;
var
  form: TMainForm;
begin
  form := Create(Application);
  try
    form.URL := AURL;
    form.ShowModal;
    Result := LongInt(form);
  finally
    FreeAndNil(form);
  end;
end;

end.

unit LauncherMainform;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw; 

type

  TShowDllForm = function(AHandle : THandle; const AUrl : String) : LongInt; stdcall;

  TMainForm = class(TForm)
    edt1: TEdit;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    LibHandle : THandle;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.btn1Click(Sender: TObject);
var
  DLLProc : TShowDllForm;
begin
  LibHandle := LoadLibrary(PChar('BrowserDLL.dll'));
  if LibHandle <> 0 then  begin
    @DLLProc := GetProcAddress(LibHandle,'ShowBrowserForm');
    if (@DLLProc <> nil) then try
      DLLProc(Application.Handle, edt1.Text);
    except
      on E:Exception do
        ShowMessage('Error Running dll.' + #13#10 + E.Message);
    end;
  end else
    ShowMessage('Error Loading dll');
end;

end.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top