Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Fr Jul 18, 2025 16:55

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 18 Beiträge ]  Gehe zu Seite 1, 2  Nächste
Autor Nachricht
 Betreff des Beitrags: Passing Objects To DLL's
BeitragVerfasst: Mi Apr 22, 2009 17:29 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
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.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mi Apr 22, 2009 18:21 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
Could this be what you want? http://www.pascalgamedevelopment.com/fo ... pic=5652.0 (but then the other way around, and a hint to what you may want to achieve)

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mi Apr 22, 2009 23:58 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
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:
  1. type  
  2.     THelloWorldI = class  
  3.       public  
  4.         function Say(): string; virtual ; CDECL; abstract ;
  5.     end;

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Apr 23, 2009 09:00 
Offline
DGL Member
Benutzeravatar

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.

Code:
  1.   // defintion of interface
  2.   MyFunnyInterface = interface
  3.     function GetCount: Integer; cdecl;
  4.   end;
  5.  
  6.   // implementation of
  7.   TMyClass = class (TInterfacedObject, MyFunnyInterface)
  8.   private
  9.     ...
  10.   public
  11.     constructor Create;
  12.  
  13.     function GetCount: Integer; cdecl;
  14.   end;


Usage of the interface.
Code:
  1. function CreateInterface: MyFunnyInterface;
  2. begin
  3.   Result :=  TMyClass.Create;
  4. end;

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.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Apr 23, 2009 18:53 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
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:
  1. Var
  2. MyClass : TMyClass;
  3.  
  4. 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).

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Apr 24, 2009 08:44 
Offline
DGL Member
Benutzeravatar

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:
  1. uses
  2.   myClass;  // << The unit which declares TMyClass. Its used in dll and application
  3.  
  4.   function CreateClass: TMyClass; external 'myClass.dll';
  5.  
  6. var
  7.   myClass: TMyClass;
  8. begin
  9.   myClass := CreateClass;
  10.  
  11.   if myClass is TMyClass then
  12.     myClass.DoAny; // << This never will be called
  13.  
  14.   if myClass = TMyClass then
  15.     myClass.DoAny; // The same
  16. 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:
  1. uses
  2.   myInterface; // deklaration of interface
  3.  
  4. function CreateInterface: MyFunnyInterface; external 'myInterface.dll';
  5.  
  6. var
  7.   myInterface: MyFunnyInterface;
  8. begin
  9.   myInterface := CreateInterface;
  10.  
  11.   writeln (myInterface.GetCount);
  12.  
  13.   myInterface := nil;  // dec refcounter to maybe free the interface
  14. 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:
  1. type
  2.   IInterface1 = interface
  3.     ['{C606AFD9-CAF1-43D7-BE79-215E0CE07B2B}']
  4.     // delphi generate this GUID by pressing strg+shift+g
  5.   end;
  6.  
  7.   IInterface2 = interface
  8.     ['{1756B3E8-B6F2-4AA0-BB28-BF917DDC22AB}']
  9.   end;
  10.  
  11.   TMyClass = class(TInterfacedObject, IInterface1, IInterface2)
  12.   end;
  13.  
  14. var
  15.   Int1: IInterface1;
  16.   Int2: IInterface2;
  17. begin
  18.   Int1 := DllMethod; // This calles TMyClass.Create; inside dll
  19.  
  20.   if Int1.QueryInterface(IInterface2, Int2) = 0 then
  21.     ShowMessage('IInterface2 supported')
  22.   else
  23.     ShowMessage('IInterface2 not supported');
  24. 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.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Apr 24, 2009 11:42 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
Thanks.

Code:
  1. // delphi generate this GUID by pressing strg+shift+g

For reference strg is the ctrl key on non-german keyboards.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Fr Apr 24, 2009 12:29 
Offline
DGL Member
Benutzeravatar

Registriert: Do Dez 05, 2002 10:35
Beiträge: 4234
Wohnort: Dortmund
Stucuk hat geschrieben:
Code:
  1. // delphi generate this GUID by pressing strg+shift+g

For reference strg is the ctrl key on non-german keyboards.

Ups. Sure. :D


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Sa Apr 25, 2009 06:48 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
BTW is the GUID only needed for the QueryInterface/Etc or is it a requirement?

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Sa Apr 25, 2009 08:43 
Offline
Forenkatze
Benutzeravatar

Registriert: Mi Okt 22, 2003 18:30
Beiträge: 1945
Wohnort: Närnberch
Programmiersprache: Scala, Java, C*
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 :D So yeah, it's highly optional.

_________________
"Für kein Tier wird so viel gearbeitet wie für die Katz'."


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Sa Apr 25, 2009 08:47 
Offline
Forenkatze
Benutzeravatar

Registriert: Mi Okt 22, 2003 18:30
Beiträge: 1945
Wohnort: Närnberch
Programmiersprache: Scala, Java, C*
Lossy eX hat geschrieben:
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'."


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Sa Apr 25, 2009 14:13 
Offline
Ernährungsberater
Benutzeravatar

Registriert: Sa Jan 01, 2005 17:11
Beiträge: 2068
Programmiersprache: C++
Doesn't 7zib uses interfaces and is written in C++?

_________________
Steppity,steppity,step,step,step! :twisted:
❆ ❄ ❄ ❄ ❅ ❄ ❆ ❄ ❅ ❄ ❅ ❄ ❅ ❄ ❄
❄ ❄ ❄ ❅ ❄ ❄ ❄ ❅ ❄ ❄ ❆ ❄ ❄


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Sa Apr 25, 2009 21:32 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
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 :(.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Sa Apr 25, 2009 22:19 
Offline
Ernährungsberater
Benutzeravatar

Registriert: Sa Jan 01, 2005 17:11
Beiträge: 2068
Programmiersprache: C++
I found this article about using c++ classes from dll: http://rvelthuis.de/articles/articles-cppobjs.html
But as far as I see, this need modications on the c++ code.

_________________
Steppity,steppity,step,step,step! :twisted:
❆ ❄ ❄ ❄ ❅ ❄ ❆ ❄ ❅ ❄ ❅ ❄ ❅ ❄ ❄
❄ ❄ ❄ ❅ ❄ ❄ ❄ ❅ ❄ ❄ ❆ ❄ ❄


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: So Apr 26, 2009 15:50 
Offline
DGL Member
Benutzeravatar

Registriert: Mi Jul 21, 2004 22:39
Beiträge: 360
Wohnort: UK, Scotland
So Virtual Abstracts with Stdcall is the way to go.

_________________
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 18 Beiträge ]  Gehe zu Seite 1, 2  Nächste
Foren-Übersicht » English » English Programming Forum


Wer ist online?

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.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.009s | 14 Queries | GZIP : On ]