1

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
If r = DRNG_SUCCESS Then
    ' do something
Else
    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.'

1
  • Both C# and VB versions seem wrong. Commented Mar 13, 2020 at 22:07

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.