Link to home
Start Free TrialLog in
Avatar of drama22
drama22

asked on

Activex loadlibrary and show parented form issue

i have dll dynamic library application it has a form inside it . i am trying to load that dll inside  and ActiveX application that runs in IE, problem is i cannot show parent form inside it . here is my  complete code

Activex code

unit loader;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, activapp_TLB, StdVcl, Vcl.ExtCtrls, ShlObj, Vcl.StdCtrls;

type
  Tactivappform = class(TForm, Iactivappform)
    mpanl: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }

  protected
       { Protected declarations }

  public
    { Public declarations }
  end;

  type
  TcreaFunc = function (Width: Integer; Height: Integer; hw:HWnd): boolean; stdcall;
  TDisFunc = procedure stdcall;
  TSetProp = procedure (Param: string; Values: string); stdcall;


implementation

uses ComObj, ComServ;

{$R *.DFM}

{ Tactivappform }



procedure Tactivappform.Button1Click(Sender: TObject);
var
  dllHandle : THandle;
  creaFunc : TcreaFunc;
  DisFunc : TDisFunc;
  SetProp : TSetProp;
  S: widestring;
  PW: PWideChar;
begin
  S := 'load.dll';
  pw:=pwidechar(widestring(s));
  dllHandle := LoadLibrary(pw);
  if dllHandle <> 0 then
  begin
    @creaFunc := GetProcAddress(dllHandle, 'createApplication');
    @DisFunc := GetProcAddress(dllHandle, 'displayClient');
    @SetProp := GetProcAddress(dllHandle, 'SetProperties');
    if Assigned (creaFunc) then
    begin
      creaFunc(mpanl.Width, mpanl.Height, mpanl.Handle);
      setProp('float', 'Yes');
      DisFunc;
    end
    else
      ShowMessage(' CreateApplication function not found ');
    FreeLibrary(dllHandle);
  end
  else
  begin
    ShowMessage('load.dll not found / not loaded');
  end;
end;




initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    Tactivappform,
    Class_activappform,
    0,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);
end.

Open in new window



my dll code


library load;


uses
  System.SysUtils,
  System.Classes,
  Themes,
  Dialogs,
  Windows,
  Forms,
  Controls,
  Unit1 in 'Unit1.pas' {appform},
  neededfunc in 'neededfunc.pas',
  constant in 'constant.pas',
  ParamClass in 'ParamClass.pas',
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

var
  mHandle: THandle;    // Mutexhandle
  DLLHandle: Longint = 0; { this var will hold the DLL's Handle }


function createApplication(Width: Integer; Height: Integer; hw:HWnd): boolean; stdcall;
begin


    mHandle := CreateMutex(nil, True, 'Appnameda-Inst');
    if GetLastError = ERROR_ALREADY_EXISTS then
    begin
      ShowMessage('app already opened');
      Halt;
    end;

  try
    Application.Handle := hw;
    appform := Tappform.Create(Application);
    appform.Top := 0;
    appform.Left := 0;
    appform.Width := Width;
    appform.Height := Height;
    appform.Align := alClient;
    Windows.SetParent(appform.Handle, hw);
    Result := True
  except
    on e: exception do
    begin
      ShowMessage('ERROR creating : ' + e.Message);
      Result := False;
    end;
  end;
end;

procedure closeApplication; stdcall;
begin
  ApplicationClosed := True;
  try
    if mHandle <> 0 then CloseHandle(mHandle);
  except
  end;
  if Assigned(appform) then try FreeAndNil(appform); except end;
  try OptimizeRamUsage; except end;
end;

procedure SetProperties(Param: string; Values: string); stdcall;
begin
  with Params do
  begin
    try  if Param = 'float' then uname := Values; except end;
  end;
end;

procedure displayClient; stdcall;
begin
  appform.Show;
end;

procedure DLLEntryProc(EntryCode: Integer);
begin
  case EntryCode of
    DLL_PROCESS_DETACH:
  begin
    ThemeServices.Free;
  end;
    DLL_PROCESS_ATTACH:
  begin

  end;
    DLL_THREAD_ATTACH:
  begin

  end;
    DLL_THREAD_DETACH:
  begin

  end;
  end;
end;

exports
  closeApplication,
  createApplication,
  SetProperties,
  displayClient;

begin

  DllProc := @DLLEntryProc;


end.

Open in new window


sadly i cant show the form inside dll into activex panel , i tested the code on exe application and it works fine i dont know why it wont work in activex
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drama22
drama22

ASKER

thank you sinisa , any idea why when i close the internet explorer app still running ? and what  about if i want to call SetProperties or any other export function later ? i mean what about if i want to call another function to set some records inside that dll later after dll loaded ?
There is no problem calling SetProperties or any other functions from dll. Just check if dllHandle <> 0.
What do you think your app is still running after IE closed?
Avatar of drama22

ASKER

solved sinisa Vuk you are a boss thank you
Avatar of drama22

ASKER

Sinisa anoth thing why when i try to reload the Ie the application crash ? i already call Halt in form destroy
think that you need closeApplication in finalization part and before you do FreeLibrary - to detach dll form.
finalization
   if dllHandle <> 0 then 
   begin
      @CloseAppFunc := GetProcAddress(dllHandle, 'closeApplication');
      CloseAppFunc;
      FreeLibrary(dllHandle);  //release dll
   end; 

Open in new window