?
Solved

Creating controls at runtime through DLL

Posted on 2003-12-03
2
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
snehanshu earned 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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