gpt4 book ai didi

c# - 如何在 C# 中使用的以 C++ 编写的外部 .dll 资源上正确创建入口点

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:37 25 4
gpt4 key购买 nike

长期读者,第一次海报。有一天我希望能在这里回答问题...

所以它有点类似于:"Unable to find an entry point named [function] in dll" (c++ to c# type conversion)

但我似乎无法应用相同的解决方案...

基本上,我写了一个新方法:

在C++项目的头文件中定义为:

extern "C" {
__declspec(dllexport) bool IsDataValid();
}

在 C++ 项目的源文件中定义为:(仅签名)

extern bool __cdecl IsDataValid() { 
//function stuf......... returns a bool
}

导入到 C# 项目中的表单 C# 应用程序中:

[DllImport("CarChipSDK_C_Sharp.dll", EntryPoint = "IsDataValid")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsDataValid();

它是从 C# 表单 .cs 文件中的相同位置调用的:

bool isDataValid = IsDataValid();

它正在返回一个异常消息:

"Unable to find an entry point 'IsDataValid()' named in DLL 'CarChipSDK_C_Sharp.dll'.

我在从 C++ 代码生成的 .dll 上使用了 dumpbin.exe 和 dependency walker,它表明它具有 IsDataValid() 入口点。

非常感谢所有帮助...

问题解决了!愚蠢的我,这是我当前公司以前合作社的代码,结果他正在从 bin/release 文件夹中读取 .dll,而我正在构建到 bin/debug 文件夹。早该知道。诚挚的歉意。

最佳答案

您遇到了 C++ 名称重整。将 C++ 函数声明为 extern "C"。所以,在你的 C++ 模块中......

extern "C" __declspec(dllexport) bool IsDataValid();

您实际上也不需要入口点规范属性。您的 C# 声明将是:

[DllImport("CarChipSDK_C_Sharp.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsDataValid();

为了将来的引用,dumpbin.exe 是一个非常有用的程序,用于分析此类问题。如果您在 DLL 上运行它,您将看到这些函数在编译时实际命名的内容。

关于c# - 如何在 C# 中使用的以 C++ 编写的外部 .dll 资源上正确创建入口点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com