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

发布于 2024-10-12 10:14:33 字数 1190 浏览 4 评论 0原文

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

所以它有点类似于: “无法在 dll 中找到名为 [function] 的入口点”(c++ 到 c# 类型转换)

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

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

在 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();

它返回异常并显示以下消息:

“无法找到入口点 DLL 中命名的“IsDataValid()” 'CarChipSDK_C_Sharp.dll'。

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

非常感谢所有帮助...

问题已解决!愚蠢的我,这是我现在公司的前一位合作伙伴的代码,结果他正在从 bin/release 文件夹中读取 .dll,而我正在构建到 bin/debug 文件夹。应该知道。我真诚地道歉。

Long time reader, first time poster. One day I hope to be answering questions on here...

So it's kind of similar to: "Unable to find an entry point named [function] in dll" (c++ to c# type conversion)

But I can't seem to apply the same solution...

Basically, I wrote a new Method:

Defined in the header file of the C++ project as :

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

Defined in the source file of the C++ project as: (signiature only)

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

Imported into a forms C# application within the C# Project as:

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

It is called from the same place within C# forms .cs file as:

bool isDataValid = IsDataValid();

It is returning an exception with the message:

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

I have used dumpbin.exe and dependency walker on the .dll generated from the c++ code and it shows that it has the IsDataValid() entry point.

All help is much appreciated...

Problem Solved! Stupid me, this was the code from a previous co-op at my current company, turns out he was reading the .dll from the bin/release folder where as I was building to the bin/debug folder. Should have known. My sincere apologies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-10-19 10:14:33

您遇到了 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 上运行它,您将看到这些函数在编译时的实际名称。

You are encountering C++ name mangling. Declare the C++ functions as extern "C". So, in your C++ module...

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

You really don't need the entry point specification attribute either. Your C# declaration will be:

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

For future reference, dumpbin.exe is a very useful program for analyzing problems like this. If you run it on your DLL you will see what those functions are actually named by the time they are compiled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文