C++/VB - Exporting Classes from DLLs
Asked By Mike
02-Aug-07 03:08 PM

A couple of weeks ago in this newsgroup (or it may have been the un-managed
version of this group), someone was asking whether they should export a
create/destroy an instance of a C++ class defined by the DLL. I'm interested
in understanding how I can use this technique. I have the following sample...
DLL Pseudocode...
export long createMyObject(void ** ppObjInst);
export long destroyMyObject(void ** ppObjInst);
class MyObject //NB: No export declaration
{
/* blah blah */
};
A simple tester of this code, allows me to create and destroy an instance of
MyObject. However, if I try to access a member function of MyObject the
linker balks with 2019 error.
It seems like my only choice is to export the class and all will be well as
I've experimented with however I'm curious to know if I'm missing another
possibility. Ideally, I would like to do a dumpbin on the binary and see a
clean list of create/destroy functions.
Is there a way to inject the C++ class definitions into the library without
polluting the export list?
Thanks,
Mike
PpObjInst
(1)
DLLs
(1)
DestroyMyObject
(1)
CreateMyObject
(1)
CreateIt
(1)
MyObject
(1)
CallFoo
(1)
PSmth
(1)
Alex Blekhman replied...
This is unmanaged version.
Besides exporting a class you can make common abstract base
class and implement it inside DLL. Like this:
// Common header file (used both by DLL and EXE)
//
class ISomething
{
public:
...
virtual ~ISomething() {};
virtual int Foo(int param) = 0;
virtual void Delete() = 0;
};
// Export only one function:
//
__declspec(export/import) ISomething* CreateIt(...);
//// End of common header
// DLL implementation (.CPP file)
//
class CSomething : public ISomething
{
public:
...
virtual int Foo(int param) { ... }
virtual void Delete() { delete this; }
};
ISomething* CreateIt(...)
{
...
return new CSomething();
}
In EXE you call `CreateIt' to get pointer to `ISomething'
interface and use it:
ISomething* pSmth = CreateIt();
int a = pSmth->Foo(42);
...
pSmth->Delete();
pSmth = NULL;
You could get away making `CreateIt' returning `void*',
however, it's not type safe and error prone.
Alex
David Wright replied...
You could always add...
__declspec(export/import) CallFoo( ISomething* );
etc...
for every function you wish to call on the Object? But it is better to
expose the class....
Alex Blekhman replied...
Of course you can do it. But it is the opposite of what Mike
wants. If I understand correctly, his goal is to use a
class, which is implemented inside DLL without making new
exports (either whole class or many C functions).
Alex
Mike replied...
Thanks Alex, you are right...I'm not interested in exporting the entire class
or individual members of a class.
I tested out your earlier suggestion...thanks...that's what I was looking for.
Cheers,
Mike
Ben Voigt [C++ MVP] replied...
Using dllexport on C++ class or its members is a recipe for trouble. Making
the call through the v-table is the correct thing to do.
Alexander Nickolov replied...
Just curious, why not travel the path to its logical end and make
it a COM DLL?
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
Alex Blekhman replied...
See comments inline.
Actually, you don't need `DestroyObject', since an object
can destroy itself with member function.
[...]
Use of two sequential underscore characters ( __ ) at the
beginning of an identifier, or a single leading underscore
followed by a capital letter, is reserved for C++
implementations in all scopes. It is best to avoid using
such names.
`GetProcAddress' accepts LPCSTR (i.e., ANSI string) as
second parameter.
Non standard names again.
I like this approach because a class is more self-contained
this way.
Alex
aslan replied...
I knew I would get this comment. Actually I use "m_" in the projects I
develop code for money. It's my personnel projects that I have this habit of
using "__" instead of "m_" (a typing ease, I guess).

Internatinalization and multiple language support without resource DLLs C++ / VB Hello I'd like to support multiple languages for my MFC application (GUI elements, message strings, menu items). I don't want to use the resource DLLs because my client want to provide additional languages without neccesity of recompilnig new dll per I have googled but didn't find any other ways for MFC than using resource DLLs. Is there a standard solution for this? Thanks Dominolog VC MFC Discussions TwoGuysInAGarageWithADog (1) Windows in messageboxes). For resources like dialogs etc it is recommended to create so called 'satellite dlls' for each language that contain the resources for each language. There are many tutorials available schrieb: Hi Dominolog, appTranslator is your friend: Among many other goodies, it creates the resource DLLs for you. Your client won't need a compiler to create them. They just type d have to load each control at run time. The nice thing about resource (satellite) DLLs is that you can add them when you want them and leave them out if such that his client must not / can not use the compiler to build the localized DLLs. So, I think that Serge's appTranslator tool can be the answer to the OP small so it will not matter that much. Tom Yes. The standard solution is resource DLLs. It is considered "best standard practice" and there are good reasons for it. Any other
R" statically loads my unicode extension dll "E" * "E" itself statically loads two MBCS extension dlls written by a external vendor. Unfortunately, my regular DLL "R" (and hence, the two extension the problem be that "R" and "E" use the MFC90UD.DLL whereas the external extension dlls use "MFC90D.DLL"? Thanks mk VC MFC Discussions Windows 7 (1) Vista (1) FinishDlabDLL (1 LoadLibraryA (1) LoadLibraryW (1) Why not? Have you used Depends to check if all necessary DLLs are present? I do not see any immediate reason why it should not - but we app or get external vendors libs with the version that you are using. With Extension DLLs you are going to have issues immediately if different version of MFCs are used in figure out how to make this work in some other fashion, such as acquiring updated DLLs from the third party, or reverting everything to ANSI / MBCS. But on the whole, I also an application of an external vendor. It can be extended by registering plug-in dlls which then are loaded at startup. it is definitely an ANSI application. But it is to work for most configurations. Provided I could persuade the vendor of the ansi extension dlls to build a unicode version, would the module dependencies would look like this: Ansi exe answer. R is a plug-in used to extend A. A and the other extension dlls are written by external vendors. I am not very familiar with MFC. So, I ask
t read the article but I can tell for sure that you can use resource dlls with static MFC. Actually, this is how appTranslator is implemented. Oh, and if you're looking for the easiest and smartest way to create your resource DLLs, well, just follow the URL in my sig ;-) HTH, Serge. http: / / www.apptranslator.com - Localization bar. Also, I would add that providing translation of MFC resources in your own resource dlls is easier than redistributing localized MFC dlls. The time when we could simply copy these shared DLLs to \ windows \ system32 is over. You're left with the option of copying everything in your app directory (hence provide side-by-side dlls), which to say the least decreases the interest of a shared dll. My 2 EUR user's machine. During the installation of such a program, the installer will overwrite settings, DLLs and environment variables that sometimes cause all of the other software on the machine to more self-contained, I don't have a need to assure myself that the dependent DLLs are up-to-date and consequently don't have a need to carry them along
out and need your helpful advice on: when my user application starts, it dependants (system DLLs) get loaded (if needed) and they get mapped in. When this takes place, how do do not see how the link could help me. Don't put copies of system DLLs in your app folder. Re. current directory: Looking in current directory is considered non-safe from the system dir. @Pavel, i'm afraid you are right regarding not having those dlls in the app's folder at load time, but users are also clients and i would know that what causes it is a problem w / windows itself: unfortunately, some system dlls' behavior depends on where the lib was loded from. i mean the case when cwd com Providenza & Boekelheide, Inc. so, you are saying my only choice is to reload those dlls. right? i cannot move the dlls: they are in use. i can move the application, but the situation may repeat itself next time the app loads; i can not prevent system dlls from re-appearing in the app's folder. i can hope that one day all bugs in all system dlls will be fixed by ms. I'd be surprised if you did not run into
Torsten VC MFC Discussions Office (1) Adobe (1) BrY8j.17270 (1) PNGLib (1) CreateDialogIndirect (1) DLLs (1) SizeofResource (1) FindResource (1) It's possible to "manually" define a dialog resource with might want to have a single .exe, with everything inside, and not an exe + several DLLs. Giovanni In which case he has to ensure that resource IDs don't clash. The question is *why* does he not want DLLs, as this is the solution to the problem. There may be a good reason. The only good reason not to have DLLs which I have come across is that in Win98 you can't use the MS Layer for Unicode without recompiling the MFC DLLs. With static linking of MFC that is not a problem. But there may be others suppose. [My solution was to forget Unicode until I could forget Win98, and keep the DLLs, which are convenient for so many other reasons.] Dave - - David Webber Author of 'Mozart the There are trade-offs, pros and cons, of course. If an app is factorized in DLLs, it's easier to upgrade only some modules (e.g. a graphics 3D engine stored thing could happen for MFC or C / C++ run-time; if we dynamically link using DLLs, if Microsoft updates these C / C++ or MFC DLLs, we can have the benefits "automagically