سؤال

In a Windows project we define an interface class

class Interface {
public:
    virtual ~Interface() { }
    virtual void func() = 0;
};

and a factory function

__declspec(dllexport) Interface *construct();

inside a DLL. Of course there is a concrete specialization of Interface inside the DLL but we don't export it. Nevertheless we can use it outside the DLL. How does this work? The vftable of the instance constructed by construct() consists of function pointers targeting functions which aren't exported by the DLL. Is this approach acceptable or more or less a hack?

هل كانت مفيدة؟

المحلول

There just isn't any need to export the concrete class. The v-table you get from the Interface pointer will be set to the concrete v-table stored inside the DLL. Which already has all the function pointers set correctly to the implementation methods. So once you got the v-table pointer then you're golden and can call any of the concrete methods. Compare with COM, you never export anything from a COM server, other than the factory function (DllGetClassObject).

You do have to export the construct() function, there isn't any other way that external code can obtain the address of this function. A lookup table is required that maps the function name to the function address. The export table of the DLL. Exporting functions and classes add entries to that export table.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top