krish
krish

Reputation: 95

why to mention not to mangle the name when calling a c++ function from other c++ dll

This is my exported function in dll1

        extern"C" __declspec(dllexport) int FUN1(char* p){
        return p[0];
        }

    I am calling this FUN1 from other project.Below is the code

        #include
        #include
        using namespace std;
        typedef int (*MYFUN1)(char*);

        int main()

            {

                HMODULE hMod = LoadLibrary ("C:\\Users\\admin\\Documents\\Visual Studio 2010\\Projects\\CalledFun\\Debug\\CalledFun.exe");
                if(hMod != NULL)
                {
            MYFUN1 pCtor = (MYFUN1) GetProcAddress (hMod, "FUN1");
            int a = pCtor("calling a value") ;
            cout<<a;
                }
            }

If I remove the extern "C" from dll1 then that function address is returning as NULL when calling GetProcAddress in dll2. Since both are written in c++ I thought name mangling will not effect this.I thought if we use c and c++ libraries combinely then only we need to mention extern "C" ,Can anyone help me to get out of this confusion

Thanks in advance

Upvotes: 1

Views: 245

Answers (1)

Roman Kolesnikov
Roman Kolesnikov

Reputation: 12147

You need to disable name mangling because you are using Windows API like LoadLibrary to which you provide function name. Windows API an LoadLibrary in particular know nothing about your compiler name mangling, so you need to turn it off.

Upvotes: 1

Related Questions