Hello all,
I'm writing a program that uses drivers for people counting devices, in
Delphi 7. The drivers are stored in BPLs, and must be loaded
dynamically, since I don't know in advance which devices will be used at
a site, and of course do not wish to recompile the application every
time a new device is introduced.
 
The architecture is such that all drivers inherit from a superclass
driver. The superclass driver is used in both the app and each package.
 
I manage to load the package dynamically (the HModule <> 0), and procede
to load the class, using an example from Delphi.
 
Unfortunately, when I try to use GetClass it returns nil. I register the
class as follows.
 
Any ideas?
 
Sample code is provided:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
unit MyClassUnit;
 
interface
 
uses Classes, Dialogs, SuperClassUnit;
 
type
  TMyClass = class(TSuperClass)
  published
    procedure Execute(aString: String); override;
 
    property MyResult;
  end;
 

implementation
 
{ TMyClass }
 
procedure TMyClass.Execute(aString: String);
begin
  ShowMessage(aString + '+' + FMyResult);
end;
 
initialization
  RegisterClass(TMyClass); // <--- This is reached when the app runs
LoadPackage
 
end.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
unit SuperClassUnit;
 
interface
 
uses Classes;
 
type
  TSuperClass = class(TPersistent)
  private
    procedure SetMyResult(const Value: String);
  protected
    FMyResult: String;
  public
    procedure Execute(aString: String); virtual; abstract;
    property MyResult: String read FMyResult write SetMyResult;
  end;
 

implementation
 
{ TSuperClass }
 
procedure TSuperClass.SetMyResult(const Value: String);
begin
  FMyResult := Value;
end;
 
end.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.Button1Click(Sender: TObject);
type
  aMyClass = class of TSuperClass;
var
  phm: HModule;
  AClass: TPersistentClass;
  fn: String;
begin
  fn:= 'MyClass';
  phm:= LoadPackage(fn+'.bpl');
  if phm <> 0 then
  begin
    AClass:= GetClass('T'+fn);   //  <------------------- Returns nil
    if AClass <> nil then
    with aMyClass(AClass).Create as TSuperClass do
    begin
      MyResult:= 'Bull';
      Execute('Pit');
      Free;
    end;
    UnloadPackage(phm);
  end;
end;
 
end.
Assaf Stone
Spacelogic ltd.
[EMAIL PROTECTED]
Tel: 972-9-8855565 ext. #315
 


[Non-text portions of this message have been removed]



-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to