Solved

Creating controls at runtime through DLL

Posted on 2003-12-03
2
282 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi component that can load a DLL in design time? 8 64
Delphi : could not find program, '...exe' 2 171
CheckListBox usage 3 59
Breakpoint doesn't stop in my variable 3 28
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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