Sign in to follow this  

Extern "C" with referances: Why does this work?

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was playing around with DLLs and wondered something:
//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;
}



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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
__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.

Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this