Solved

Registering class in DLL.

Posted on 2000-03-02
7
1,795 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
[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
  • 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
Industry Leaders: 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!

 
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

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month3 days, 11 hours left to enroll

630 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