1

Passing classes across DLL boundaries is a bad idea due to undefined vtable layout in c++, but what if I explicitly set the calling convention, and avoid virtual functions and inheritance?

In other words, could i safely pass pointers to the following struct across a DLL?

struct MyStruct {

    int a;
    int b;

    WINAPI MyStruct(int a, int b)
        : a(a), b(b)
    {}

    void WINAPI SetA(int a) {

        this->a = a;
    }
};

Would it be safe to use link to such a DLL with a differetnt compiler version etc?

2
  • I don't see what could go wrong if all involved code was compiled with the same compiler and options, even with vtables and inheritance...
    – PlasmaHH
    Jun 7, 2013 at 8:39
  • 1
    If your functions are inline then the object layout should be the same if you ensure correct packing/alignment (e.g. with #pragma pack) then the function SetA will be compiled into the app 'hosting' the dll. The other option is to manually create a vtbl of function pointers.
    – Pete
    Jun 7, 2013 at 8:41

2 Answers 2

2

Your code is really equivalent to the following C code:

struct MyStruct {
    int a;
    int b;
};

void WINAPI InitMyStruct(struct MyStruct* p, int a, int b)
{
    p->a = a; p->b = b;
}

void WINAPI MyStruct_SetA(struct MyStruct* p, int a)
{
    p->a = a;
}

Avoiding virtual functions has basically bought you nothing; it is still compiler-dependent what these "equivalent" C functions will be called ("name mangling") so you need to use compatible compilers. All versions of MSVC from this millennium are compatible with each other in this respect. All versions of GCC from this millennium are compatible with each other. Just don't mix the two (link-time errors will happen).

There are other sources of problems:

Make sure your packing/alignment settings match up (but they need to do that for plain C interfaces, as well).

If you use "new" in one DLL and "delete" in another, you might be in trouble unless you use the exact same compiler version and use the DLL runtime library. So don't new or delete your MyStruct object from client code; instead, provide functions inside the DLL to do that for you.

Stay away from standard library containers in your interface. They won't work if the DLL and the client aren't linked to the same standard library.

Don't be afraid of virtual functions.

Note: all these problems exist in theory on other platforms as well, but appear to be a little less relevant in practice for Linux and Mac OS X.

1

Neither DLL boundaries nor vtables are special in any way. Release unit boundaries are.

If you mix objects compiled with different compilers, different compiler versions, or sometimes different compiler options in one executable, there's a real possibility to get incompatible object layouts in different parts of the program. This could happen if you use DLLs or static linking, and with any kind of object, vtables or not.

So if your executable and all of its DLLs are compiled and released together as a whole, you have nothing to worry about. If the executable and the DLLs are different products, released separately by independent organizations, you need to be very careful. It is wise to only use C-compatible data structures across release unit boundaries, and never free memory allocated in a different unit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.