如何在VB.NET或C#中使用C++API

How to use C++ API in VB.NET or C#

本文关键字:C++API VB NET      更新时间:2023-10-16

我需要在.NET中嵌入一个用C++编写的API,指纹设备使用该API到.NET应用程序中。

我尝试了VB.NET和C#这两种语言,但仍然无法理解它给我带来的问题;

在DLL中找不到入口点。

这意味着它访问DLL,但问题是什么,你能告诉我吗?

这是示例代码:

internal static class UnsafeNativeMethods
{
    const string _dllLocation = "ABCAPI.dll";
    [DllImport(_dllLocation, EntryPoint = "ABCFunc", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl    )]
    public static extern bool ABCFunc();
}
[System.Runtime.InteropServices.DllImport("ABCAPI.dll", SetLastError = true)]  
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]  
public static extern bool ABCFunc();

谢谢@Hans Passant我做了一个研究和使用"dumpbin/Exports mydllname.dll"找到函数的实际声明,这最终对我有效。

internal static class UnsafeNativeMethods {
const string _dllLocation = "ABCAPI.dll";
[DllImport(_dllLocation, EntryPoint = "_ABCFunc@4", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.ThisCall    )]
public static extern bool ABCFunc();

}

而且效果很好,谢谢你的建议。