[DllImport("C:\\gaul-windows.dll", ...)]属性を介して C++ ライブラリを C# ソリューションに使用しています。
パラメータの一部がデリゲートである関数を使用する必要があり、それが返す型にもフィールドとしていくつかのデリゲートがあります。
これはできますか?
structを戻り値の型として使用しようとしたため、私は尋ねましたが、 「メソッドの型シグネチャは PInvoke と互換性がありません」という例外が発生しました。
だから私はそれを変更し、戻り値の型として IntPtr を使用しようとしましたが、次の例外が発生しました: 'パラメーター #16' をマーシャリングできません: ジェネリック型をマーシャリングできません。
まず、それが可能かどうかを知りたいですか?この種の機能をそのように使用すること。それが不可能な場合、どのように使用できますか?
編集
使用する必要がある機能
[DllImport("C:\\gaul-windows.dll",
SetLastError = 真、
CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr ga_genesis_boolean(intpopulation_size,
int num_chromo,
int len_chromo、
GAgeneration_hook ジェネレーション_フック、
GAiteration_hook iteration_hook,
GAdata_destructor data_destructor,
GAdata_ref_incrementor data_ref_incrementor,
GAevaluate 評価し、
Gシードシード、
GAadapt 適応、
GAselect_one select_one,
GAselect_two select_two,
GA変異変異、
GAクロスオーバー クロスオーバー、
G交換交換、
整数?ユーザーデータ);
これはデリゲートの 1 つの例です。
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate short GAselect_one(
参照人口ポップ、
IntPtr 母);
こちらでクラス全体を取得できます。
そして最後に、これは関数への私の呼び出しです
var x = Gaul.ga_genesis_boolean(30, /* const int 人口サイズ */
vehicle_num, /* const int num_chromo */
order_num, /* const int len_chromo */
IntPtr.Zero,// null, /* GAgeneration_hook generation_hook */
null, /* GAiteration_hook iteration_hook */
null, /* GAdata_destructor data_destructor */
null, /* GAdata_ref_incrementor data_ref_incrementor */
new GAevaluate(drp_score),/* GAevaluate 評価 */
new GAseed(Gaul.ga_seed_boolean_random), /* GAseed シード */
null, /* GAadapt 適応 */
new GAselect_one(Gaul.ga_select_one_bestof2),/* GAselect_one select_one */
new GAselect_two(Gaul.ga_select_two_bestof2),/* GAselect_two select_two */
new GAmutate(Gaul.ga_mutate_boolean_singlepoint), /* GAmutate mutate */
new GAcrossover(Gaul.ga_crossover_boolean_singlepoints), /* GAcrossover クロスオーバー */
null, /* GReplace 置換 */
null /* vpointer ユーザーデータ */
);