tags:

views:

32

answers:

1

Hi,

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?

A: 

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
Does this mean I need to do this: Private Shared Function Func() As <MarshalAs(UnmanagedType.Bool)> Boolean
Shoko
Yes. But that's not your problem, the unmanaged code is destroying the heap.
Hans Passant
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.
Shoko
As it turned out, the fix was to marshal it the way I did in my first comment.
Shoko
Excellent. Thought that might be the case :)
OJ