Solved

Creating controls at runtime through DLL

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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