문제

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.)

도움이 되었습니까?

해결책 2

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.

다른 팁

Wrap the offenging function in another C++ function, and declare it with extern "C". No need to create a special DLL for it, just include one C++ file in your project.

You could investigate

LoadLibrary("path to dll");

to load the DLL and

GetProcAddress("?f@@YAXXZ");

to grab a function pointer to the externally declared function.

I do not see any clean solution besides creating an additional dll written in C++ and exposing all interfaces via extern "C".

You could compile your C code using the same C++ compiler they used, then your C functions will be mangled using the same mechanism and everything will link seamlessly, and no-one will notice any difference.

If you must use a different compiler, then you'll have to manually load the dll using LoadLibrary and each function using GetProcAddress.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top