www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D ABI and C++ COM-like interface


I got a quite extensive code base written in C++ (~1.5Mil lines of code), so
porting all of that to D is not an option.

However all the code use a consistent object model that looks like somewhat like
come. So a C++ interface is defined for ALL objects and modules that can be
implemented. There's an introspection API that allows to list all methods,
properties, interfaces, and objects defined in each modules. This allows for
easy integration with other languages, including writting modules in other

So far there's a complete interop layer for the .NET platform, and for a
LUA-like language called squirrel. Both these can access 100% of the API written
in C++ and also implement interfaces.

Considering that D has access to low level assembly, I would think that it would
be easy to write a small library that contains the few low-level entry points of
the object model. That is :
iUnknown* CreateInstance(ObjectType)
bool ImportModule(Name,Flags,...)

That's all that is needed.

Now the "tricky" part is to make sure that I can redefine the interfaces in D
and make sure that their VTable and ABI is the same as a C++ VTable.

If it's already the case, then it should be a piece of cake. The only thing
would be to write a D interface :
// In D
interface iUnknown {
int AddRef();
int Release();

Now the methods of the interfaces all use __stdcall as calling convention,
although it could be changed if it's necessary.

Worst case I can write a function "CallMethod" in D and then generate a wrapper
class for each interface that would use that method (there is one in C++ that is
used by the Squirrel interop). Now that would be quite a bit slower than calling
the C++ interface directly obviously.

So the question is, does anybody knows if the ABI for the vtable and calling
convention for those methods are the same as C++ stdcall, and if not, is at
least the vtable at the same position (if not it could be possible to add a
dummy method to offset the vtable), and if the calling convention is __stdcall,
if not I can change the calling convention but I need to know what D uses...

Also, considering that the object model is fixed, and there isnt two ways of
calling the methods of those interfaces, I was thinking that I could aswell take
the GDC compiler and add an extension to the D language that would make sure
that those C++ interfaces can be called easily. That would allow a good level of
interop for any C++ library aswell as for my particular object model.

Best Regards,
- Pierre
Dec 27 2005