DIFFICULT: C++ class exporting & __declspec(dllexport)
Posted on 2004-08-20
Problem: Dynamically link C++ classes into my application at run-time to provide specific ustomized behavior. E.g. in my case, I have several formats of file to read, say F1 & F2. Currently, my app has a class F1Reader that reads F1 and creates a bunch of other objects of type Factor1, Factor2, Factor3, etc. I want to add support for a new file type F2, which will produce the same type of Factor1,2,3 objects, but with different data. Factor objects are already well-defined C++ classes within my application. Therefore, F1Reader & F2Reader are class factories. Further, I would like to be able to add F3 in the future without modifying my primary application. That is I want to be able to provide F3Reader.DLL & some registry settings, and boom, it works.
One obvious solution would be to encapsulate all the Factor type objects as well as the F1Reader & F2Reader classes as COM objects. This is certainly straight-forward, but it is a LOT of work considering the amount of current code that uses Factor1,2,3 objects. Therefore, I'm looking for a solution that will continue to create native C++ objects with dynamically linked code, without creating COM wrappers.
My proposed solution is to refactor F1Reader & F2Reader so they derive from a common base (FactorFactoryBase), and put F1Reader & F2Reader in separate C++ DLLs that might or might not be present on the user's machine. The interface to FactorFactoryBase will provide all my application needs to create the Factor objects.
Consider this little attempt to test out the concept:
struct __declspec(dllexport) B
m_x = 1;
const type_info& t = typeid(*this);
m_s = t.name();
returns a compiler warning:
c:\dev\test\cppdll\b.h(12) : warning C4251: 'm_s' : class 'std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' needs to have dll-interface to be used by clients of struct 'B'
While this is only a warning, it is a level 1 warning, and sounds pretty fatal since I want the DLLs to derive from B.
It seems I need to instantiate an specific instance of string and export that. But I haven't been able to figure out how.
class __declspec(dllexport) stringEx: public basic_string<char>
& got the same warning message.
Any suggestions on how to solve this problem?