是否可以使用;C++;图书馆职能;当它的某些参数是委托时,它会被转换为C#吗? 通过C++语言将C++库应用到C语言解决方案中 [DllImport(“C:\\gaul windows.dll,…)]属性
我需要使用一个函数,它的一些参数是委托,它返回的类型也有一些委托作为字段 我能做这个吗 我问这个问题是因为我试图使用是否可以使用;C++;图书馆职能;当它的某些参数是委托时,它会被转换为C#吗? 通过C++语言将C++库应用到C语言解决方案中 [DllImport(“C:\\gaul windows.dll,…)]属性,c#,c++,interop,dllimport,gaul,C#,C++,Interop,Dllimport,Gaul,我需要使用一个函数,它的一些参数是委托,它返回的类型也有一些委托作为字段 我能做这个吗 我问这个问题是因为我试图使用结构作为返回类型,但是我得到了一个异常:“方法的类型签名与PInvoke不兼容” 因此,我更改了它并尝试使用IntPtr作为返回类型,但随后出现了以下异常:无法封送'parameter#16':无法封送泛型类型。 所以首先我想知道这是否可能?以这种方式使用这种函数。如果不可能,我将如何使用它 编辑 我需要使用的函数 [DllImport("C:\\gaul-windows.dll"
结构作为返回类型,但是我得到了一个异常:“方法的类型签名与PInvoke不兼容”
因此,我更改了它并尝试使用IntPtr作为返回类型,但随后出现了以下异常:无法封送'parameter#16':无法封送泛型类型。
所以首先我想知道这是否可能?以这种方式使用这种函数。如果不可能,我将如何使用它
编辑
我需要使用的函数
[DllImport("C:\\gaul-windows.dll",
SetLastError = true,
CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr ga_genesis_boolean(int population_size,
int num_chromo,
int len_chromo,
GAgeneration_hook generation_hook,
GAiteration_hook iteration_hook,
GAdata_destructor data_destructor,
GAdata_ref_incrementor data_ref_incrementor,
GAevaluate evaluate,
GAseed seed,
GAadapt adapt,
GAselect_one select_one,
GAselect_two select_two,
GAmutate mutate,
GAcrossover crossover,
GAreplace replace,
int? userdata);
[DllImport(“C:\\gaul windows.dll”,
SetLastError=true,
CallingConvention=CallingConvention.Cdecl)]
公共外部静态IntPtr ga_genesis_boolean(int population_size,
int num_chromo,
国际色球联合会,
GAgeneration_hook generation_hook,
迭代钩迭代钩,
GAdata_析构函数数据析构函数,
伽达图参考增量数据参考增量,
评价,,
油籽,
自适应,
气体选择一个选择一个,
气体选择二选择二,
甘氨酸突变,
交叉交叉,
换装,
int?用户数据);
这是一个代表的例子
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate short GAselect_one(
ref population pop,
IntPtr mother);
[非托管函数指针(CallingConvention.Cdecl)]
公共代表-短GASU-one(
参考人口流行,
IntPtr(母亲);
你可以得到全班同学
最后,这是我对函数的调用
var x = Gaul.ga_genesis_boolean(30, /* const int population_size */
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(darp_score),/* GAevaluate evaluate */
new GAseed(Gaul.ga_seed_boolean_random), /* GAseed seed */
null, /* GAadapt adapt */
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 crossover */
null, /* GAreplace replace */
null /* vpointer User data */
);
var x=高卢、高卢、高卢和布尔(30,/*常数整数总体大小)*/
车辆数量,/*const int num\u chromo*/
顺序数,常数长度*/
IntPtr.Zero,//null,//GAgeneration\u hook generation\u hook*/
null,/*GAiteration\u hook迭代\u hook*/
null,/*GAdata_析构函数数据_析构函数*/
空,/*GAdata\u ref\u incrementor数据\u ref\u incrementor*/
新GAevaluate(darp_分数),/*GAevaluate*/
新种子(Gaul.ga_种子\u布尔\u随机),/*种子*/
空,/*GAadapt自适应*/
新GAselect_one(Gaul.ga_select_one_Bestof 2),/*GAselect_one select_one*/
新的GAselect_two(Gaul.ga_select_two_Bestof 2),/*GAselect_two select_two*/
新GAmutate(Gaul.ga_mutate_boolean_singlepoint),/*GAmutate mutate*/
新GAcrossover(Gaul.ga_crossover_boolean_singlepoints),/*GAcrossover crossover*/
空,/*换装替换*/
null/*vpointer用户数据*/
);
DllImportAttribute
只能应用于方法定义。该方法定义定义了DLL中声明的本机函数的签名。这意味着该方法必须是全局函数(并导出),并且不支持类函数
不能对struct和class等类型定义使用DllImportAttribute
如果您想从不属于全局函数的DLL导入方法,您必须创建全局函数来包装您想要导入的内容。
< P>是的,当某些参数为委托时,可以使用C++库函数。p>
我收到的错误“无法封送”参数#16:无法封送泛型类型。”与委托参数无关,而是可为空的整数。这是一个CLI/C++库吗?不,不是。它是C++中编译的一组函数,意在通过C++项目中的命令行导入,而用户数据应该是ItpTR。如果您将population声明为类,则可以使用它返回函数。我还建议您使用此工具clrinterop.codeplex.com/releases/view/14120我正在尝试使用一个全局函数,它使用另一个全局委托函数作为参数。如果没有代码,我无法具体告诉您出了什么问题。但是,如果您有一个本机结构,则需要在C#中声明该结构,以便告诉封送处理程序该结构在内存中的具体结构。这不可能是一个普通的问题