c++ - 即使在使用 extern "C"之后,如何解决名称重整出现的问题?

标签 c++ c dll name-mangling borland-c++

为了避免 C++ 代码的名称重整问题,我在头文件中使用了 extern "C"。但是,问题仍然存在“当我使用 Borland C++ IDE 构建我的 dll 文件时”。

下面是我的示例代码。

其他.h文件

#define H_EXPORT WINAPI  

  #ifdef __cplusplus

  extern "C" {

  #endif

  long H_EXPORT RegOpenKeyEx32( DWORD hKey,LPCSTR lpSubKey,DWORD ulOptions,REGSAM   samDesired,DWORD FAR *phkResult);

  #ifdef __cplusplus

  }

  #endif

其他.cpp文件

  #define C_EXPORT WINAPI _export
  //The function has been define like this

  long C_EXPORT RegOpenKeyEx32( HKEY      hKey,
                              LPCSTR     lpSubKey,
                              DWORD      ulOptions,
                              REGSAM     samDesired,
                              PHKEY      phkResult)
  {

   //some code
  }

在 def 文件中为该函数给出的序数值是

RegOpenKeyEx32 @243

但是,在构建 dll 之后,我使用 Dll Export Viewer 公开了 dll,它的序数值更改为 85,函数名称更改为 @RegOpenKeyEx32$qqsp6HKEY__pxcululpp6HKEY__ 并且函数被像这样破坏 p>

enter image description here

在同一个文件中,即 others.cpp 只有一些函数名(5 个函数名)被破坏,其余函数名相同(没有被破坏)。我不明白这是什么问题?

知道是什么问题请回复...

最佳答案

声明和定义都必须使用 extern "C"

您可以修改Others.cpp文件如下

#define C_EXPORT WINAPI _export

#ifdef __cplusplus
extern "C" {
#endif
long C_EXPORT RegOpenKeyEx32(HKEY    hKey,
                             LPCSTR  lpSubKey,
                             DWORD   ulOptions,
                             REGSAM  samDesired,
                             PHKEY   phkResult)
{
    //some code
}
#ifdef __cplusplus
} // extern "C"
#endif

关于c++ - 即使在使用 extern "C"之后,如何解决名称重整出现的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695200/

相关文章:

c - 如何使用 char 将字符串传递给 fopen C

适用于 UWP 和经典桌面的 C++ 通用 Windows DLL

c++ - 在 Visual Studio 2012 中调试 C DLL 文件时无法单步执行代码

python - 多态性和pybind11

c - GCC 为什么会生成错误的结构偏移量?

c++ - 将 vector 插入另一个 vector

c - 如何在 SDK 的交叉编译器中访问 -ldl

c++ - 如何避免Windows上多个CRT版本的问题(重新讨论了dll?)

c++ - 在 C++ 类中访问 getter setter 中的结构变量

c++ - 如何在 C++ 中构造带参数的对象