【问题标题】:Stackoverflow exception when calling dll which uses a static lib调用使用静态库的 dll 时出现 Stackoverflow 异常
【发布时间】:2015-05-27 15:18:37
【问题描述】:

我编写了一个使用 C++ 静态库的 C# 应用程序。我已将 Lib 包装在 .dll 中,但遇到了问题。当我调用 .dll 函数时,我得到一个 System.Stackoverflow 异常。

我必须使用 .lib,因为它已经在其他地方使用,我需要能够让我通过 C# 程序与 lib 进行通信的解决方案。

C#

CreateRegistryLocation(mPath);

[DllImport("Wrapper.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int CreateRegistryLocation(string key);

C++ .dll

extern "C" 
{
   __declspec(dllexport) int CreateRegistryLocation(const char* key)
   {
      return RegistryNamespace::CreateRegistryLocation(key);
   }
}

C++ .lib

extern "C"
{
   int CreateRegistryLocation(const char* key);
}

如果我从 dll 中删除对 lib 的调用,那么 dll 和应用程序之间的通信似乎可以按我的预期工作。我已经删除了 .lib 中的主体并使其仅返回 0,所以我没有相信 lib 函数的主体与问题有关。我没有将返回值分配给 C# 中的任何内容,因此我看不到递归问题。目前代码应该只是调用 exe -> Dll -> Lib 并且值 0 应该返回到链上。

非常感谢任何见解。

【问题讨论】:

  • 如果你在你的库的 CreateRegistryLocation 函数中放置一个断点,它会到达那里吗?如果是,请检查您的参数是否正确编组...

标签: c# visual-studio-2010 dllimport dllexport


【解决方案1】:
__declspec(dllexport) int CreateRegistryLocation(const char* key)
{
    return RegistryNamespace::CreateRegistryLocation(key);
}

可能的解释是该函数位于RegistryNamespace 命名空间中,因此RegistryNamespace::CreateRegistryLocation 只是一个非终止递归调用。鉴于问题中的代码,这是最合理的解释。

【讨论】:

  • 感谢 David,我没有意识到 .dll 位于命名空间内,并且正在浪费我的时间试图提出更奇特的解释。
猜你喜欢
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2015-11-02
相关资源
最近更新 更多