【发布时间】: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