Home >Backend Development >C++ >How to Control Exported Function Names in C DLLs: Decorated vs. Undecorated?

How to Control Exported Function Names in C DLLs: Decorated vs. Undecorated?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-17 01:32:03733browse

How to Control Exported Function Names in C   DLLs: Decorated vs. Undecorated?

C DLL Exports: Decorated and Undecorated Names

When exporting functions from a C DLL, you may encounter decorated (mangled) names. This behavior arises from the compiler's name-mangling mechanism for resolving symbol names in C .

Module Definition File (.def)

When using a Module Definition file, you are explicitly specifying the names of the exported functions. However, the compiler still adds a decorated suffix to the exported symbol names. This is the mangled version of the function name, which includes information about the function's arguments, return type, and other details.

extern "C" Export

Exporting functions using the "extern "C"" syntax prevents the compiler from name-mangling the function names. However, it does not remove the additional suffix after the "=".

Pragma Comment

An alternative approach to explicitly export functions is to use the #pragma comment linker directive. This directive allows you to specify the decorated function name to be exported. For example:

#pragma comment(linker, "/EXPORT:SomeFunction=_SomeFunction@@@23mangledstuff#@@@@")

This directive instructs the linker to export the "SomeFunction" function with the provided decorated name.

FUNCTION Macro

Another option is to use the FUNCTION macro within the function's body. This macro expands to the undecorated function name. You can then use the pragma comment directive to export the function with the decorated name, as follows:

#pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)

This technique allows you to specify the decorated name without explicitly specifying it.

By following any of these approaches, you can control the exported function names and avoid the undesired "=" and decorated suffix that may appear when using the .def file or the "extern "C"" syntax.

The above is the detailed content of How to Control Exported Function Names in C DLLs: Decorated vs. Undecorated?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn