Freetype .net wrapper problems (attempted to write protected memory exception)

Go To StackoverFlow.com

0

I am trying to setup freetype2 for .net and I am still not lucky enough. So I am using the answer from this question. At the moment I try to use the Init_FreeType function I get an exception that I attempted to write to protected memory. The code I use is the following :

Intptr library = new Intptr();
FreeType.FT.Init_FreeType(library);

And the declaration of Init_FreeType function in the wrapper is the following :

[DllImport(FT_DLL, EntryPoint = "FT_Init_FreeType"), SuppressUnmanagedCodeSecurity]
public static extern int Init_FreeType(IntPtr /*IntPtr LibraryRec_*/ alibrary);

Any ideas?

2012-04-05 19:38
by muku


1

Looks to me like the declaration for Init_FreeType is wrong. It should probably be:

public static extern int Init_FreeType(ref IntPtr alibrary);

(The parameter is a ref). And to call it:

IntPtr library = IntPtr.Zero;
FreeType.FT.Init_FreeType(ref library);

I suspect what's happening is that Init_FreeType is treating the passed value as a reference and trying to store the library handle at that memory location. But since you're passing the value of the pointer (rather than the pointer location), it's going off into the weeds.

Further info in response to comment:

Documentation for FT_Init_FreeType defines it as:

FT_EXPORT( FT_Error ) FT_Init_FreeType( FT_Library  *alibrary );

FT_Error is an int, and FT_Library is

typedef struct FT_LibraryRec_  *FT_Library;

So you definitely do need the ref on the call. The managed prototype should be as I showed above.

The stack imbalance is almost certainly caused by the wrong calling convention. In .NET, the default calling convention is stdcall. It looks to me like FreeType is using the cdecl calling convention. So your DllImport should be:

[DllImport(FT_DLL, EntryPoint="FT_Init_FreeType", 
    CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
2012-04-05 22:18
by Jim Mischel
Well I just tried that but I doubt this is the problem. I got the following message when running the code over the Init_FreeType : "PInvokeStackImbalance was detected. This is likely because the managed PInvoke signature does not match the unmanaged target signature - muku 2012-04-06 01:50
@muku: See my updated response - Jim Mischel 2012-04-06 02:33
Indeed I changed my code as in your updated answer and it now works. Now I have to change almost every function in the wrapper according to your suggestion because other functions throw the same exceptions as well.. Thanks : - muku 2012-04-06 15:31
Ads