Solved

Registering class in DLL.

Posted on 2000-03-02
7
1,771 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
Comment Utility
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
Comment Utility
correction:

var GetDllClass : function : TPersistentClass = nil;

0
 
LVL 9

Author Comment

by:ITugay
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
 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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

7 Experts available now in Live!

Get 1:1 Help Now