c++ - vb.net 将数组传递给 C++ 导致堆栈错误

标签 c++ vb.net dllimport

我有一些最初是用 C++ 作为 lib 文件编写的代码。我已将其转换为 dll 并让一些例程正常工作,但是当我点击传递数组的函数时,我收到堆栈错误。我不是 C++ 程序员,但从我读过的内容来看,'uint32_t*' 意味着它必须作为引用传递,但我不确定该怎么做。我曾尝试将 ByRef x() 作为 IntPtr,将 ByRef x 作为 Uint32 等。它也可能是暗淡的声明,但我不确定。

这是代码:

' C++ declaration
extern __declspec(dllexport) int rdrand_get_n_32(unsigned int n, uint32_t* x);

' VB.net dll import
<DllImport("drng.dll", CallingConvention:=CallingConvention.Cdecl)>
Public Shared Function rdrand_get_n_32(ByVal n As Integer, ByRef x As IntPtr) As Integer
End Function

' C# equivalent
[DllImport("drng.dll", CallingConvention=CallingConvention.Cdecl)]
public extern static int rdrand_get_n_32(int n, ref IntPtr x);

' vb code
Dim array32(9) As Integer

r = rdrand_get_n_32(RDRandCutoff, array32)      ' This line errors
If r = DRNG_SUCCESS Then
    ' do something
Else
    Console.Write("rdrand instruction failed with code {0:D}" & vbLf, r)
End If


错误:
托管调试助手'FatalExecutionEngineError':'运行时遇到 fatal error 。错误地址位于线程 0x3058 上的 0x716b9e97。错误代码为 0xc0000005。此错误可能是 CLR 中的错​​误或用户代码的不安全或不可验证部分中的错误。此错误的常见来源包括 COM-interop 或 PInvoke 的用户编码(marshal)错误,这可能会损坏堆栈。

最佳答案

我发现出了什么问题。
dllimport 必须是。

' VB.net dll 导入

公共(public)共享函数 rdrand_get_n_32(ByVal n As Integer, Byval x as uint32()) As Integer
结束功能

关于c++ - vb.net 将数组传递给 C++ 导致堆栈错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60677437/

相关文章:

c++ - 链接问题 C++、glew 和 SDL

c# - 在 .Net 中,在键盘、上键或下键中编写按键代码的更好选择是什么?

vb.net - 首先使用 Entity Framework 5 数据库进行依赖注入(inject)。入门?

c# - 从 C++ DLL 调用 C# 方法/函数(从 C# 使用 "Dllimport"加载)

c++ - C++ 方法的 Visual Studio 2010 工具提示注释

c++ - 动态数据结构(如链表)的意义是什么

c++ - 一个通用的 STLish contains()

VB.NET LINQ 按多列分组

c# - 尝试创建与非 .NET 应用程序一起使用的 .NET DLL

c++ - 从 C++ 调用 DLL 中的函数