PDA

View Full Version : Derive from an Class in a DLL



technomage
21-10-2006, 04:14 PM
I have just been thinking about this particular problem.

I figured out how to share a class outside a DLL with another application. This is done by declaring an abstract class in a "interface" unit. both the main application and the dll reference this unit, there is an additional function on the dll to return and instance of the class within the DLL. All of this requires a shared memory unit so the whole application using the same memory manager.

Now why do I have a problem :?: :?:

I want to be able to derive a new class from one that is implemented in another DLL. Like you can with Delphi packages. There must be a way of doing this, anyone got any ideas :?:

Clootie
21-10-2006, 05:14 PM
Wait until packages are implementd in FPC?

Let's see how it's impelmented behind your code. When your class funcition calls "inherited" it's really a static call to TBaseClass_OutFunction(Self, parameter1, ...). So if you are compiling a plain DLL - compiler just don't know where destination funciton is located (unless you build in that function - i.e. compile it in the same DLL).

Sure you can try to avoid this using the same "interfaces" trick. I would go by "implements" route, but IIRC FPC does't support this [correctly?].

Sergey K.
07-04-2007, 07:03 AM
I've implemented this kind of thing once via FPC. The basic idea is that you patch a VMT entry of the instantiated class. Code goes like this:




Function tObjectsLinker.IntroduceVMT(VMT:Pointer):String;
Var ParentVMT:Pointer;
ParentClass:tLClass;
Begin
While Assigned(VMT) Do Begin
ParentVMT:=tClass(VMT).ClassParent;
If Not Assigned(ParentVMT) Then Exit;
If FindClass(tStaticClass(ParentVMT).ClassName,Parent Class) Then Begin
If ParentClass.PackageID=CORE_PACKAGE Then Begin
Logger.Log('Class '+tStaticClass(VMT).ClassName+' expands '+ParentClass.ClassName);
pPointer(VMT+vmtParent)^:=ParentClass.StaticClass; // do injection
Exit(ParentClass.ClassName);
End;
End;
VMT:=tClass(VMT).ClassParent;
End;
Logger.Fatal('Unable to determine class ancestor');
End;

Procedure tObjectsLinker.IntroduceClass(Var LClass:tLClass);
Var ParentVMT:Pointer;
// VMT:Pointer;
// ParentClass:tLClass;
Begin
If LClass.PackageID=CORE_PACKAGE Then Begin
// don't touch Core classes
// FIXME:
ParentVMT:=LClass.StaticClass.ClassParent;
If Assigned(ParentVMT) Then LClass.ExpandsClass:=tStaticClass(ParentVMT).Class Name Else
LClass.ExpandsClass:='';
Exit;
End;
LClass.ExpandsClass:=IntroduceVMT(LClass.StaticCla ss);
End;




You could find my solution at http://www.linderdaum.com in old downloads

marcov
10-04-2007, 11:32 AM
Sure you can try to avoid this using the same "interfaces" trick. I would go by "implements" route, but IIRC FPC does't support this [correctly?].

Sb was working on it. Could be that it is in devel version (the future 2.2)