Solved

Registering class in DLL.

Posted on 2000-03-02
7
1,785 Views
Last Modified: 2012-08-13
Hi all.

When I register class in DLL, then I can't see that class in main application.

//********Code in MYLIB.DLL:

initialization
   RegisterClass(TMyClass);
finalization
  UnregisterClass(TMyClass);
end.

//********Code in Application:
...
   LoadLibrary('MYLIB.DLL');
   if GetClass('TMyClass') = nil
      then ShowMessage('Class not found');
...

Does anybody know whats wrong?

Best regards,
Igor.
0
Comment
Question by:ITugay
  • 4
  • 2
7 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 2579268
The class is registered only in the class list of the DLL module. The application class list (which is a different one) doesn't have this class.

If your DLL and your application is written in exactly the same Delphi version, you can export a function from your dll like this:

function GetClass : TPersistentClass;
begin
  result := TMyClass;
end;

exports GetClass;

Then in your application do this:

var GetDllClass : function GetClass : TPersistentClass = nil;

var dll : dword;
begin
  dll := LoadLibrary('your.dll');
  GetDllClass := GetProcAddress(dll, 'GetClass');
  // Now you can call GetDllClass

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2579270
correction:

var GetDllClass : function : TPersistentClass = nil;

0
 
LVL 9

Author Comment

by:ITugay
ID: 2579350
Madshi,

Thanx, it's look good. Is it possible to register class from DLL to main application classes list? I try to explain what I need.

Usually, DB application, have a lot of editing/inserting records forms. My idea is very simply. Every table have predefined form for editing/inserting. I have a list of matches between table name and form classname (it stored in database). Due to this, there is an opportunity to open the forms of editing/inserting from any place of the application without including into "uses ..." clause.

Last two years it work Ok, but now, I'm trying to put some forms into DLL's. Is it possible to do this avoiding of modification any of my service units?

Regards,
Igor
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 2579435
Well, I have a nice idea for you, but it's not too simple, I hope you understand it...   :-)

Add this to your application's project file (dpr):

exports RegisterClass, UnregisterClass;

(You'll have to add something to the uses clause of the dpr perhaps)

Now in each DLL main unit you can do something like this:

var
  RegisterClass : procedure (AClass: TPersistentClass) = nil;
  UnregisterClass : procedure (AClass: TPersistentClass) = nil;

var appModule : dword;
initialization
  GetModuleHandle(nil);
  RegisterClass := GetProcAddress(appModule, 'RegisterClass');
  UnregisterClass := GetProcAddress(appModule, 'UnregisterClass');
  RegisterClass(TMyClass);
finalization
  UnregisterClass(TMyClass);
end.

That should do all the work! Now in your application you only need to call LoadLibrary('yourDll.dll') and then the dll puts it's class automatically into the class list of the application.

Tricky, isn't it!?   :-)

Regards, Madshi.

P.S: But remember: The DLLs and the application MUST be compiled with the same Delphi version, otherwise the TPersistentClass classes might not be compatible -> wild exceptions!
0
 
LVL 9

Author Comment

by:ITugay
ID: 2579474
Yes Madshi. The great idea is to export procedures from main App to DLL:). You are realy tricky.

initialization
  AppModule:= GetModuleHandle(nil); ??;)

Thanx for help, I try it as soon as possible, but points is yours right now.

Cheers,
Igor.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2579504
 AppModule:= GetModuleHandle(nil);

This gives you the module handle (= instance handle) of the application, which is needed to call GetProcAddress.
0
 

Expert Comment

by:ekoindri
ID: 24565416
I tried the code looks like :

unit UFAdminSFCSTPPRDCDBGDG;

interface

uses
  UAdminSFCSTPPRDCDBGDG, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, DB, InvokeRegistry,
  Dialogs, RzStatus, RzButton, StdCtrls, Mask, RzEdit, RzLabel, RzTabs, ExtCtrls, RzPanel, CodeSiteLogging, RzCommon;

type
  TFAdminSFCSTPPRDCDBGDG = class(TFrame)
    Panel1012111010101011101010101110101710: TRzPanel;
    PC101211101010101110101010111010171010: TRzPageControl;
    Tab10121110101010111010101011101017101010: TRzTabSheet;
    Panel1012111010101011101010101110101710101010: TRzPanel;
    PC101211101010101110101010111010171010101010: TRzPageControl;
    Tab10121110101010111010101011101017101010101010: TRzTabSheet;
    RzLabel31: TRzLabel;
    RzLabel47: TRzLabel;
    IN101211101010101110101010111010171010101010101010: TRzEdit;
    Tmb1012111010101011101010101110101710101010101010: TRzBitBtn;
    Tab10121110101010111010101011101017101010101011: TRzTabSheet;
    RzLabel76: TRzLabel;
    RzLabel77: TRzLabel;
    Tmb1012111010101011101010101110101710101010101110: TRzBitBtn;
    IN101211101010101110101010111010171010101010101011: TRzEdit;
    Tab10121110101010111010101011101017101010101012: TRzTabSheet;
    Tmb1012111010101011101010101110101710101010101210: TRzBitBtn;
    RzPanel6: TRzPanel;
    RzPanel18: TRzPanel;
    RzLabel78: TRzLabel;
    Tab10121110101010111010101011101017101011: TRzTabSheet;
    Panel1012111010101011101010101110101710101110: TRzPanel;
    Panel101211101010101110101010111010171010111010: TRzPanel;
    RzProgressStatus5: TRzProgressStatus;
    RzPanel27: TRzPanel;
    RzLabel79: TRzLabel;
    PC101211101010101110101010111010171010111010: TRzPageControl;
    Tab10121110101010111010101011101017101011101010: TRzTabSheet;
    Tmb1012111010101011101010101110101710101110101010: TRzBitBtn;
    Tmb1012111010101011101010101110101710101110101011: TRzBitBtn;
    Tmb1012111010101011101010101110101710101110101012: TRzBitBtn;
    Tmb1012111010101011101010101110101710101110101013: TRzBitBtn;
    Tmb1012111010101011101010101110101710101110101014: TRzBitBtn;
    Tmb1012111010101011101010101110101710101110101015: TRzBitBtn;
    Tmb1012111010101011101010101110101710101110101016: TRzBitBtn;
    Panel101211101010101110101010111010171010111011: TRzPanel;
    Tab10121110101010111010101011101017101012: TRzTabSheet;
    Panel1012111010101011101010101110101710101210: TRzPanel;
    Panel101211101010101110101010111010171010121010: TRzPanel;
    PS10121110101010111010101011101017101012101010: TRzProgressStatus;
    RzPanel59: TRzPanel;
    RzLabel80: TRzLabel;
    PC101211101010101110101010111010171010121010: TRzPageControl;
    Tab10121110101010111010101011101017101012101010: TRzTabSheet;
    Tmb1012111010101011101010101110101710101210101010: TRzBitBtn;
    Tmb1012111010101011101010101110101710101210101011: TRzBitBtn;
    Tmb1012111010101011101010101110101710101210101012: TRzBitBtn;
    Panel101211101010101110101010111010171010121011: TRzPanel;
    RzPanel60: TRzPanel;
    RzPanel61: TRzPanel;
    RegIniFile: TRzRegIniFile;
  private

    { Private declarations }
  public
    { Public declarations }

    constructor Create(EKO : TComponent); override;
  end;

var
  FAdminSFCSTPPRDCDBGDG : TFAdminSFCSTPPRDCDBGDG;

implementation

{$R *.dfm}

constructor TFAdminSFCSTPPRDCDBGDG.Create(EKO: TComponent);
begin
  inherited create;
end;

CodeSite.ExitMethod( 'procedure : AssignDoubleBuffered' );
end;
var
  RegisterClass : procedure (AClass: TPersistentClass) = nil;
  UnregisterClass : procedure (AClass: TPersistentClass) = nil;
  appModule : dword;

initialization

appModule := GetModuleHandle(nil);
RegisterClass := GetProcAddress(appModule, 'RegisterClass');
UnregisterClass := GetProcAddress(appModule, 'UnregisterClass');
RegisterClass(TFAdminSFCSTPPRDCDBGDG);

finalization

 UnRegisterClass(TFAdminSFCSTPPRDCDBGDG);

end.

But still error with raise error message : EAccessViolation
Any solution or advise please ?

thanks for advance
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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