Solved

Registering class in DLL.

Posted on 2000-03-02
7
1,784 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 100
drawing animated level bar based on numbers 3 97
Multiple image collision 13 75
how to update exe applicatio from internet ? 6 78
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

778 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