Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DLLS

Posted on 1997-03-23
1
Medium Priority
?
222 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 200 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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

782 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