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]