Solved

DLLS

Posted on 1997-03-23
1
211 Views
Last Modified: 2010-04-04
hi
I'm trying to create a project that use some sort of plugins, in the form of dlls.

Example: if my project is done and running and a month from now I want to
add a feature to it, what I would like to do is just add a plugin(dll).
    What I have done is I create a dll that add a menu to my main program.
(please see the code for the Dll at the in of the Email)
then when I run my main program its load the Dll and the menu item is there.
 This I have done using the external function method.
     But for a real plugin system to work this method will not work for me.
but the Run-Time Method will.
   But I keep getting a memory error when I try this method. if anyone can
find the time to look at my code(which is very small) and perhaps tell me
what I'm doing wrong or were I can get more infomation on this subject I
would appreciate it.

thank you Mike C

--------------- Code for Dll --------------------------------------
uses
  Menus, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs;

type
  TEvent = procedure(Sender: TObject) of object;
  TSomeClass = Class
  procedure DynaClick(Sender: TObject);
end;

 var
  NewMenu : TMenuItem;
  SomeClass : TSomeClass;
  Event : TEvent;

procedure TSomeClass.DynaClick(Sender: TObject);
begin
  showMessage('Its Dll 2');
end;

procedure EntryPoint(AMenu: TMenu); export; stdcall;
var
  NoItem  : TMenuItem;
begin
   //create a event handler...
  SomeClass := TSomeClass.Create;
   //assign Event to DynaClick to be pass to the menu Onclick event...
 Event := SomeClass.DynaClick;

// add new menu to the file menu...

 NoItem := AMenu.Items[AMenu.Items.Count - 1];
 NewMenu := TMenuItem.Create(NoItem);
 NewMenu.Caption := 'DLL2 Menu';
 NewMenu.OnClick := Event;
 NoItem.Insert(NoItem.Count, NewMenu);
 SomeClass.Free;
end;

exports
   EntryPoint;

------------------------ Main Program --------------------------------

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 Menus;

  type proc = procedure(AMenu:TMenu);

type
    TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    TestLocal1: TMenuItem;
    procedure TestLocal1Click(Sender: TObject);
  private
    { Private declarations }
     myProc : proc;
     Hinst: THandle;
     FPointer: TFarProc;
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.TestLocal1Click(Sender: TObject);
begin

Hinst :=LoadLibrary('c:\files\mikes\projects\dll\Project1.dll');
if Hinst > 32 then;
   begin
    FPointer := GetProcAddress(Hinst, 'EntryPoint');
      if FPointer <> Nil then
        begin
          MyProc := proc(FPointer);
          try
            MyProc(MainMenu1);;
          except
           FreeLibrary(HInst);
           ShowMessage('Error in Calling EntryPoint Function in DLL');
          end;
       end;
     end;
end;

end.


0
Comment
Question by:mikec459
1 Comment
 
LVL 3

Accepted Solution

by:
sperling earned 100 total points
ID: 1334847
Here's the error...
type proc = procedure(AMenu:TMenu);

Should be:
type proc = procedure(AMenu:TMenu); stdcall;

The declaration you use will make Delphi pass parameters to the function as it would to any other function in your applications code, that is by using registers. However, your DLL expects parameters to be passed on stack (stdcall).

Regards,

Erik.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

20 Experts available now in Live!

Get 1:1 Help Now