Search code examples
c++cvisual-studioname-mangling

Calling C++ function from C without using extern "C"


Is it possible to call a function in a C++ DLL from C code? The function is not declared extern "C". An ugly platform dependent hack that only works with Visual Studio is fine.

Calling conventions should not be a major issue, but how do I deal with name mangling.? For instance with Visual Studio, a C++ function with signature void f() has the mangled name ?f@@YAXXZ and that is not a legal C identifier.

(You don't need to tell me that I should declare the C++ function as extern "C". I already know that. But I'm in a situation where I cannot change the C++ code.)


Solution

  • To make your compiler to statically link a function with a different exported name may be tricky. But you can always load the DLL with LoadLibrary and then use GetProcAddress.