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

StackOverflow https://stackoverflow.com/questions/10034915

  •  29-05-2021
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top