I often use Delphi to write some tools and applications, in order to expand convenience, most will be made into plug-in form.
For some reason, my plugin had to be done with other development tools, such as VC.
So there is a big problem to be solved: how to make D and VC communicate with each other and operate with each other.
The most common practice is to define some methods, VC write DLL to export these methods, D to load DLL calls.
But the problem is that a little bit larger application, this way is very cumbersome, and not intuitive.
So took a little time to study the D and VC to share the interface and the object of some methods, now will be the key to share published, hope to be useful to everyone.
Basic matters:
In D and VC, the calling convention of the interface to be shared, the method in the object must be stdcall
1. Use of Class
Both Delphi and VC use abstract classes, and the methods are defined as pure virtual methods, and the declaration order of the members should be consistent.
It is important to note the Delphi class method. General static class method in the VC directly skip can, virtual class method in the VC defined as the general virtual function can be.
D:
Ttestobj = Class
Public
class procedure Foo;
Procedure Update (intf:itestintf); Virtual stdcall; Abstract
Procedure free; Virtual stdcall; Abstract
End
Vc:
Class Itestobj
{
Public
virtual void __stdcall Update (itestintf* pintf) = 0;
virtual void __stdcall free () = 0;
};
2. Interface
D's iinterface/iunknown, defined in VC as interface/*class*/: public IUnknown, the declaration order of members should be consistent
Note that the in-D interface supports attribute definitions, but VCs do not, so the attribute definitions in the D interface are placed at the end of the declaration
If the instantiation of the interface is in the VC, a little interesting little detail to note, see the code in the following download comments
D:
itestintf = interface
[' {781e6521-8768-4ada-b843-445ece548c27} ']
function Gettext:pansichar; stdcall;
Procedure SetText (Avalue:pansichar); stdcall;
function Getvalue:integer; stdcall;
Procedure SetValue (Avalue:integer); stdcall;
Property Text:pansichar read GetText write SetText;
Property Value:integer read GetValue write SetValue;
End
Vc:
Interface Declspec_uuid ("781e6521-8768-4ada-b843-445ece548c27")
Itestintf:public IUnknown
{
Public
Virtual LPCSTR __stdcall GetText () = 0;
virtual void __stdcall SetText (LPCSTR lpszmsg) = 0;
virtual int __stdcall GetValue () = 0;
virtual void __stdcall SetValue (int value) = 0;
};
In the sample code, an interface is implemented in D to provide a VC DLL, an interface is implemented in the VC DLL, and a class is provided for use in D.
Code download
Delphi and VC share interfaces and objects