我有一些最初是用 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/