Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Registering class in DLL.

Posted on 2000-03-02
7
Medium Priority
?
1,836 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
Independent Software Vendors: 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

886 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