我有一些代码最初是用C ++作为lib文件编写的。我已经将其转换为dll并具有一些正常的例程,但是当我点击传递数组的函数时,出现了堆栈错误。我不是C ++程序员,但是从我读过的'uint32_t *'的意思来看,它必须作为参考传递,但我不确定确切如何做。我已经将ByRef x()尝试为IntPtr,将ByRef x尝试为Uint32等。它也可能是dim语句,但我不确定。
这里是代码:
' 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-interop或PInvoke的用户封送处理错误,这些错误可能会破坏堆栈。'
我弄清楚出了什么问题。dllimport必须是。
'VB.net dll导入公共共享函数rdrand_get_n_32(ByVal n为整数,Byval x为uint32())作为整数结束功能