Solved

DLLS

Posted on 1997-03-23
1
214 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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