//header:
extern "C" __declspec(dllexport) void SomeFunc(std::map<std::string, bool>& retval);
//cpp
void SomeFunc(std::map<std::string, bool>& retval)
{
retval.insert("Blah", true);
retval.insert("Stuff", false);
}
//application
void (*lpSomeFunc)(std::map<std::string, bool>&);
int main()
{
HANDLE dll = LoadLibrary("SomeDLL");
lpSomeFunc SomeFunc=GetProcAddress(dll, "SomeFunc");
std::map<std::string, bool> Test;
SomeFunc(Test);
if(Test.find("Blah"))
{
std::cout << "Worked."
}
return 0;
}
Extern "C" with referances: Why does this work?
I was playing around with DLLs and wondered something:
And it works. extern "C" is supposed to make the next line (or several lines with brackets) into C code, right? So why do referances work? Classes also work when you use a referance or a pointer. Why? Does the extern "C" just convert all pointers and referances to void* or something?
No.
extern "C" is a linkage specifier. It means that the compiler will perform C-style name-mangling on the declared item, instead of C++ -style name mangling. There's probably a few other things it does, but I can't think of 'em off the top of my head.
extern "C" is a linkage specifier. It means that the compiler will perform C-style name-mangling on the declared item, instead of C++ -style name mangling. There's probably a few other things it does, but I can't think of 'em off the top of my head.
So why won't it allow me to return a std::map if it's only name mangling, only let me referance or point to it?
__declspec(dllexport) instructs the compiler/linker to add an entry to the export table of the pe file. Without getting into the details of the pe file format (which you probably should investigate), there are three parts to each of these entries - the function address, the function ordinal number and the function name.
You might want to grab the free program at dependencywalker.com. It dumps the dlls loaded into a given exe and has a feature to toggle the display of C++ name mangling. Check out one of the msvcrt dlls to see a mixture of C++ and C exports. There are also a few data exports too (iirc).
At any rate, GetProcAddress returns the address of the targeted function. It's just a raw pointer that has to be cast as a function with a particular signature. That job is handled by the compiler because the compiler that has to contend with forming the code that passes the arguments to the function/method or contend with further manipulation of a returned object.
You might want to grab the free program at dependencywalker.com. It dumps the dlls loaded into a given exe and has a feature to toggle the display of C++ name mangling. Check out one of the msvcrt dlls to see a mixture of C++ and C exports. There are also a few data exports too (iirc).
At any rate, GetProcAddress returns the address of the targeted function. It's just a raw pointer that has to be cast as a function with a particular signature. That job is handled by the compiler because the compiler that has to contend with forming the code that passes the arguments to the function/method or contend with further manipulation of a returned object.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement