-1

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

user2724058
  • 318
  • 5
  • 20
  • 2
    No you don't want to do that. That just makes your life pointlessly hard. Shipping an extra DLL. Handling COM registration. The web browser control is already in a system COM object. The Delphi part is a simple wrapper. Handling forms in DLLs leads to multiple VCLs and needs care to get right. So, no, don't do this. Include the `TWebBrowser` control in your applications. DLL hell is bad enough without you choosing to inflict it upon yourself. – David Heffernan Feb 05 '14 at 08:29
  • I don't understand what you mean by not wanting the web browser control "to be drag on each of my client app". You don't want drag-and-drop to and from it? You don't want the window to be moveable over your other windows? ...? – David Feb 05 '14 at 09:10
  • Actually I want to implement the oAuth2 Implict grant flow with OPenAM to get the access token for this I need the logic to handle the redirection and login page for openAM using TWebBrowser. I want all the logic to be in a component so that clienst Just need to call single API GetAccessToken and all complexity is handled by the DLL it self. – user2724058 Feb 05 '14 at 11:00
  • That's fine. No point putting it in a DLL though. – David Heffernan Feb 05 '14 at 11:51
  • It means I need to put the TWebBrowser control in to all my client apps and write all the same logic why not i use a DLL? Any suggestions – user2724058 Feb 05 '14 at 12:27
  • You do not really need to use `TWebBrowser` to implement oAuth. All you really need is an HTTP client component/library to programmably send HTTP requests and receive their responses, and an HTML parser to extract values from HTML login forms. – Remy Lebeau Feb 05 '14 at 16:57
  • @user I put the VCL in all my apps and don't re-write logic. Put your code in a unit and share that unit between all your projects. You do not want to use a DLL. It will cause you confusion and headaches. It is a terrible idea. – David Heffernan Feb 05 '14 at 19:05
  • And yes, don't use a GUI control to implement oauth – David Heffernan Feb 05 '14 at 19:07

1 Answers1

3

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.
Hugh Jones
  • 2,706
  • 19
  • 30