RE: [boost] shared_ptr, interface and dlls

extern "C" InterfacePtr DLLCreate( void ) { return InterfacePtr( new InterfaceImpl ); }
At the moment this code doesn't compile because VS7.1 complains that it a function with extern "C" linkage can't return a shared_ptr<Interface> instance. How could I implement the desired behavior?
Either omit the extern "C" or, if you must use a C-style interface, do this:
extern "C" Interface* DLLCreate() { return new InterfaceImpl; }
extern "C" void DLLDestroy( Interface* p ) { delete p; }
and then construct an InterfacePtr( DLLCreate(), DLLDestroy ). You'll need a public virtual destructor in this case, though.
When writing plugins under linux (i.e. with 'dlopen' etc.), I believe I have already used a function returning a std::auto_ptr as entry point for the loader. What is wrong with that ? Is your advice above simply due to a limitation with MSVC ? Regards, Stefan

Stefan Seefeld wrote:
When writing plugins under linux (i.e. with 'dlopen' etc.), I believe I have already used a function returning a std::auto_ptr as entry point for the loader. What is wrong with that ? Is your advice above simply due to a limitation with MSVC ?
It seems so. From a quick test, g++ and bcc32 do return C++ classes from extern "C" functions, only MSVC emits an error. I'm not sure whether such a function makes sense, though. If you suppress name mangling, presumably because two different compilers mangle names differently, what are the chances that the object layout will be the same?
participants (2)
-
Peter Dimov
-
Stefan Seefeld