Creating a C DLL and exporting its functions is a common task. However, determining how exported names appear can be confusing.
When exporting functions using a Module Definition file (MyDLL.def), you may observe decorated or mangled names like "SomeFunction@@@23mangledstuff#@@@@". This occurs because C compilers decorate function symbols by adding information needed by the C run-time.
Exporting functions using the extern "C" __declspec(dllexport) syntax does not eliminate the decorated names. It only specifies that the function should have a C-style name, but the decoration remains.
To alleviate the issue, consider using the pragma directive:
#pragma comment(linker, "/EXPORT:SomeFunction=_SomeFunction@@@23mangledstuff#@@@@")
This pragma instructs the linker to export "SomeFunction" with the specified decorated name.
Alternatively, you can use the following pragma within the function body:
#pragma comment(linker, "/EXPORT:\"" __FUNCTION__ "\"=\"" __FUNCDNAME__ "\"")
This pragma uses the FUNCTION and FUNCDNAME macros to automatically retrieve the function name and decorated name.
By employing these pragmas, you can control the decoration of exported symbols, ensuring compatibility when invoking them from other languages like C#.
The above is the detailed content of How do I Export C DLL Functions and Avoid Decorated/Mangled Names?. For more information, please follow other related articles on the PHP Chinese website!