Solved

Creating controls at runtime through DLL

Posted on 2003-12-03
2
273 Views
Last Modified: 2010-04-05
Hi all

please help me to solve the problem

I am unable to create the controls to main form. I am creating these controls through the DLL procedure. Application is raising invalid memory address exception
here is the code

These are replication of my project

DLL Example:

uses
  sharemem,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,ImageHlp;


{$R *.RES}


//This is the main procedure i am creating in dll


procedure LoadPlugin(application: TApplication);

begin

   button:=TButton.Create(application.MainForm  );


   button.Caption:='Sudhakar';
   with button do
   begin

        Top:=10;
        Width:=100;
        Height:=100;
        Left:=10;
        parent:=application.mainForm;
        show;
   end;
end;
exports LoadPlugin;



Application Example
____________________

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function LoadAndRunDLLProcedure( sDLL, sFunc : string ) : boolean;
type
  // define the type of "function"
  // we're calling

  TFunc_Start = procedure(app:TApplication);
var
  Func_Start : TFunc_Start;


  hDll       : THandle;
  FuncPtr    : TFarProc;
  sMsg       : string;
begin
  Result := False;
  hDll   := LoadLibrary(
              PChar( sDLL ) );
  //app:=pApp(application);
  if(hDll > 32)then
  begin
    FuncPtr :=
      GetProcAddress(
        hDll, PChar( sFunc ) );
    @Func_Start := FuncPtr;
    if(nil <> @Func_Start)then
    begin

      Func_Start(application);
      Result := True;
    end else
    begin
      sMsg := 'DLL entry point ' +
              sFunc + ' not found';
      MessageBox(
        0, PChar( sMsg ), 'Error',
        MB_OK );
    end;
    FreeLibrary( hDll );
  end else
  begin
    sMsg := 'File ' + sDLL +
            ' not found';
    MessageBox(
      0, PChar( sMsg ), 'Error',
      MB_OK );
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
searchResult : TSearchRec;
fileName:String;
begin
                 if FindFirst('*.dll', faAnyFile, searchResult) = 0 then
  begin
    repeat
     fileName:=searchResult.Name;
     LoadAndRunDLLProcedure(
  fileName,
        'LoadPlugin' );

    until FindNext(searchResult) <> 0;

    // Must free up resources used by these successful finds
    FindClose(searchResult);
  end;

end;


end.



0
Comment
Question by:sudhakar_koundinya
2 Comments
 
LVL 5

Accepted Solution

by:
snehanshu earned 250 total points
ID: 9881624
sudhakar,
  I am not a DLL expert, but here's a solution that works for me (Delphi 7):

1. It seems FreeLibrary( hDll ) is the major cause of your problems: you need to keep the DLL loaded after calling "LoadAndRunDLLProcedure".

function LoadAndRunDLLProcedure( sDLL, sFunc : string ) : boolean;
type
  // define the type of "function"
  // we're calling

  TFunc_Start = procedure(MyCont: TApplication);
var
  Func_Start : TFunc_Start;

  hDll       : THandle;
  FuncPtr    : TFarProc;
  sMsg       : string;

begin
  Result := False;
  hDll   := LoadLibrary(
              PChar( sDLL ) );
  if(hDll > 32)then
  begin

    FuncPtr :=
      GetProcAddress(
        hDll, PChar( sFunc ) );
    @Func_Start := FuncPtr;
    if(nil <> @Func_Start)then
    begin

      Func_Start(Application);
      Result := True;
    end else
    begin
      sMsg := 'DLL entry point ' +
              sFunc + ' not found';
      MessageBox(
        0, PChar( sMsg ), 'Error',
        MB_OK );
    end;
//    FreeLibrary( hDll );//Causes problems: free later
  end else
  begin
    sMsg := 'File ' + sDLL +
            ' not found';
    MessageBox(
      0, PChar( sMsg ), 'Error',
      MB_OK );
  end;
end;

2. In the DLL, "parent:=application.mainForm;" is the culprit. I don't know the whys of these, but you can get-around this problem by replacing parent by parentwindow.

procedure LoadPlugin(MyApp:TApplication);
Var
 Button: TButton;
begin

   button:=TButton.Create(MyApp.MainForm);
   Button.ParentWindow := MyApp.MainForm.Handle;

   button.Caption:='Sudhakar';
   button.Top:=10;
   button.Width:=100;
   button.Height:=100;
   button.Left:=10;

   button.show;

end;



Hope that helps,
...Shu
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 10015663
Though it not helped me, Explaination is Good

Regards
Sudha
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now