0

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?

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
Tola Odejayi
  • 3,019
  • 9
  • 31
  • 46

1 Answers1

0

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.

OJ.
  • 28,944
  • 5
  • 56
  • 71
  • Does this mean I need to do this: Private Shared Function Func() As Boolean – Tola Odejayi Oct 17 '10 at 04:07
  • Yes. But that's not your problem, the unmanaged code is destroying the heap. – Hans Passant Oct 17 '10 at 10:15
  • Hi Hans, are you talking about the managed heap when you say 'the unamaged code is destroying the heap'? What can I do to avoid this? What should I be looking for in the unmanaged code? As far as I can tell, it does not seem to be doing anything destructive, like allocating and destroying arrays. – Tola Odejayi Oct 17 '10 at 17:30
  • As it turned out, the fix was to marshal it the way I did in my first comment. – Tola Odejayi Oct 22 '10 at 04:55
  • Excellent. Thought that might be the case :) – OJ. Oct 22 '10 at 09:56