Solved

Creating controls at runtime through DLL

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

26 Experts available now in Live!

Get 1:1 Help Now