私はこの構造体をC#でマーシャリングしようとしていましたが、最後の2行に問題があります。Marshall Cird in C#
typedef struct _modenv_
{
long lMajor; /* major version of kernel */
long lMinor; /* minor version of kernel */
long lRelease; /* release version of kernel */
long lResultSize; /* sResult buffer size */
long (__stdcall *lPGSM_ExecuteKernel) (struct _modenv_ *PGEnv, char *sCommand, char *sResult, long lLength);
long (__stdcall *lPGSM_ExecuteCommand)(struct _modenv_ *PGEnv, char *sCommand, char *sResult, long lLength);
} PGMODENV;
そして、私が行っているすべてのはこれです:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PGMODENV
{
/* input data */
public long lMajor; /* major version of kernel */
public long lMinor; /* minor version of kernel */
public long lRelease; /* release version of kernel */
/* updated data */
public long lResultSize; /* sResult buffer size */
}
どのように私はC#でそれらを実装することができますか?
私はこのようなものを見てきました。バッファーが構造体のすぐ後に続くので、構造体をメモリー内に移動できないため、マーシャリングが不可能な場合は驚くことはありません。手掛かりには、バッファサイズは長いが、バッファポインタはまったくない。 – Joshua
これらは関数ポインタです。C#での同等の機能はデリゲートオブジェクトです。注意する必要があります。渡すデリゲートオブジェクトに別の参照がある必要があります。そのため、ネイティブコードが関数呼び出しを行ったときにGCがそれらをクリーンアップしてプログラムをクラッシュさせません。静的変数に格納するか、GCHandle.Alloc()を使用してください。 –
これらの構造体は、エクスポートされた関数の引数として渡されます。これらの構造体はGCですか?どのように私は彼らが収集されるのを防ぐか? –