Solved

DLLS

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

Technology Partners: 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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

617 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