cvb.netpinvokeexecutionengineexception

ExecutionEngineException thrown when calling native code


I have a function that is exported by a C library with the following signature:

extern "C" BOOL Func()

The function is declared in VB.NET code like this:

<DllImport("mylib.dll", CallingConvention:=CallingConvention.Cdecl)>

Private Shared Function Func() As Boolean

End Function

The problem is that I get an ExecutionEngineException when I call the function from .NET code.

Given that BOOL is typedef'd as int in this C code, should the declaration be different? If so, how should I be declaring this? As Short or Int32? Do I need to marshal the return value?


Solution

  • The C function doesn't return a true boolean, it returns an integer. You might need to marshall it propery.

    Edit: Also, you should make your function static.