【发布时间】:2020-03-14 10:09:18
【问题描述】:
我有一些代码最初是用 C++ 作为 lib 文件编写的。我已将其转换为 dll 并让一些例程正常工作,但是当我点击传递数组的函数时,我收到堆栈错误。我不是 C++ 程序员,但从我读过的内容来看,'uint32_t*' 意味着它必须作为参考传递,但我不确定该怎么做。我试过 ByRef x() as IntPtr,ByRef x as 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':'运行时遇到致命错误。错误地址位于线程 0x3058 上的 0x716b9e97。错误代码为 0xc0000005。此错误可能是 CLR 或用户代码的不安全或不可验证部分中的错误。此错误的常见来源包括 COM 互操作或 PInvoke 的用户封送错误,这可能会损坏堆栈。'
【问题讨论】:
-
C# 和 VB 版本似乎都错了。