Im wondering if there is any point to put CDECL/Etc on each procedure/function of the object thats passed to a DLL. Where the DLL could be made in any programming language.
I made a Plugin system that passes classes(Well pointer to a class thats already been created) between DLL's and the main application. Each DLL(And the main app) can register classes with the plugin DLL as well as get classes from other DLL's.
Basicaly would there be any point in doing the following(Note the CDECL) or is it ultimatly pointless:
Code:
type
THelloWorldI = class
public
function Say(): string; virtual ; CDECL; abstract ;
Registriert: Do Dez 05, 2002 10:35 Beiträge: 4234 Wohnort: Dortmund
Classes in DLLs are potential problematic. The reason. Delphi Classes are only for Delphi. C++ classes are not compatible with delphi. And the application and the DLLs using his own classspace. This means TObject from DLLs are not the same like TObject from the application. If you plan to create new classes with "THelloWorldI" as parent you maybe use the opertions IS or AS. But these operations wont work with classes from other classspaces. This results because the pointers of the classes are stored in other memory reagions.
Personally i prefer the use of Interfaces. You have to define an interface and you have to create an class which implements this interface. Your DLL only returns the created interface.
Your dll crates the class and returns the interface. I thinks it's also no problem to create an dll with c++ and implement this interface. But you shouldnt use things like strings or other stuff from delphi. Interfaces from c++ dlls i have never tried before but it should work.
So would you just use the MyFunnyInterface Pointer as if it was a TMyClass on the "end" which recieves the MyFunnyInterface Pointer from the CreateInterface function?
Code:
Var
MyClass : TMyClass;
MyClass := TMyClass(MyFunnyInterfacePointer);
Zitat:
If you plan to create new classes with "THelloWorldI" as parent you maybe use the opertions IS or AS. But these operations wont work with classes from other classspaces.
THelloWorldI would be used by say the application to store the pointer of the THelloWorld object (Which would be created say in a DLL). As its virtual's it points to the DLL's memory space so you can use it perfectly fine on the application as long as everything thats created is free'd on the same end (I.E if the THelloWorld creates a PWideChar then it has to be free'd by the DLL not the application).
Registriert: Do Dez 05, 2002 10:35 Beiträge: 4234 Wohnort: Dortmund
Classes and dlls: For sure if you create an instance inside the dll you also have to free them inside the dll. All other memory (like pWideChar) has the same destiny. But i mean something different. For an example. You have an unit in which you define the class to avoid misstakes with the deklaration. You use this unit inside the dll and the application. So booth binarys uses the same classdefinition. But if your dll creates an instance of this class and return them they dont have the same classtype.
Code:
uses
myClass;// << The unit which declares TMyClass. Its used in dll and application
function CreateClass: TMyClass;external'myClass.dll';
var
myClass: TMyClass;
begin
myClass := CreateClass;
if myClass is TMyClass then
myClass.DoAny;// << This never will be called
if myClass = TMyClass then
myClass.DoAny;// The same
end;
You can call myClass.DoAny directly. But if you wants to check some classtypes this always will fail. This may be possible if you creates new classes they have TMyClass as parent.
Interfaces: Sorry. I forgot this. After you have created the interface you dont need to know the class anymore. You have to deklare an interface. This interface can be supported by different classes. The interface only describes which methods the class support. To complete my example.
Code:
uses
myInterface;// deklaration of interface
function CreateInterface: MyFunnyInterface;external'myInterface.dll';
var
myInterface: MyFunnyInterface;
begin
myInterface := CreateInterface;
writeln(myInterface.GetCount);
myInterface :=nil;// dec refcounter to maybe free the interface
end;
You dont need to know which class implements an interface. Dosnt count if its TMyClass or TMyFunnyClass or whatever. So its possible to have 2 streamclasses they only apear as one streaminterface. All Methods you need have to deklared in the interface. Interfaces also dont care if the class is implemented in pascal or c++. But for c++ you should deklare the functions from the Interface with an callingconvention (stdcall or cdecl)
One important hint. Interfaces uses an reference counter. Like strings. If no one used the interface anymore if will be freed by themself. Dont mix interfaces and the implementing classes. Interfaces will be freed automatic. So its possible that your classinstance get lost while you work with it. The implementing class dont have an refcount.
Its also possible that an class implements 2 different interfaces. With the one interface you can query the other interfaces.
Code:
type
IInterface1 =interface
['{C606AFD9-CAF1-43D7-BE79-215E0CE07B2B}']
// delphi generate this GUID by pressing strg+shift+g
Int1 := DllMethod;// This calles TMyClass.Create; inside dll
if Int1.QueryInterface(IInterface2, Int2)=0then
ShowMessage('IInterface2 supported')
else
ShowMessage('IInterface2 not supported');
end;
For QueryInterface you need these guids. So you can "attach" some info interfaces to any other interface or something. In plugins you can make an global plugin interface that describes the author, Website, and type of the plugin. Depending on the type the Interface supports the IPlugInFileformat, IPlugInDatabase or whatever for an interface.
the GUID is perfectly optional. It is solely needed for QueryInterface and those things It may be also required when using COM, but I suppose, you currently do not want to do that So yeah, it's highly optional.
_________________ "Für kein Tier wird so viel gearbeitet wie für die Katz'."
Interfaces from c++ dlls i have never tried before but it should work.
That should be absolutely impossible as C++ does not allow for interfaces at all. The best thing you could do in C++ to resemble an interface is to create an abstract class. But I don't know if the compiler is smart enough to allow those abstract classes to be used like interfaces in DLLs. Unfortunately
_________________ "Für kein Tier wird so viel gearbeitet wie für die Katz'."
So there should be no way to pass class's to DLL's created in C++ due to C++ being crap? That screws up my idea of a good plugin system that works with multple languages .
Mitglieder in diesem Forum: 0 Mitglieder und 0 Gäste
Du darfst keine neuen Themen in diesem Forum erstellen. Du darfst keine Antworten zu Themen in diesem Forum erstellen. Du darfst deine Beiträge in diesem Forum nicht ändern. Du darfst deine Beiträge in diesem Forum nicht löschen. Du darfst keine Dateianhänge in diesem Forum erstellen.