vb.net passing array to C++ causes stack error


I have some code that was originally written in C++ as a lib file. I have converted it to a dll and have some of the routines working but when I hit the function that passes an array i am getting a stack error. I am not a C++ programmer but from what I have read the 'uint32_t*' means it has to be passed as a reference but I am not sure exactly how to do it. I have tried ByRef x() as IntPtr, ByRef x as Uint32 etc. It may also be the dim statement but I am not sure.

here is the code:

' 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
    ' do something
    Console.Write("rdrand instruction failed with code {0:D}" & vbLf, r)
End If

Error: Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x716b9e97, on thread 0x3058. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'

asked on Stack Overflow Mar 13, 2020 by bgs801

1 Answer


I figured out what was wrong. The dllimport needed to be.

' VB.net dll import Public Shared Function rdrand_get_n_32(ByVal n As Integer, Byval x as uint32()) As Integer End Function

answered on Stack Overflow Mar 14, 2020 by bgs801

User contributions licensed under CC BY-SA 3.0