PInvoke - 方法的类型签名与 PInvoke 不兼容
PInvoke - Method's type signature is not PInvoke compatible
这是我试图在C#中使用的头文件签名和C代码:
__declspec(dllexport) emxArray_real_T *emxCreateWrapper_real_T(real_T *data, int32_T rows, int32_T cols);
struct emxArray_real_T
{
real_T *data;
int32_T *size;
int32_T allocatedSize;
int32_T numDimensions;
boolean_T canFreeData;
};
emxArray_real_T *emxCreateWrapper_real_T(real_T *data, int32_T rows, int32_T
cols)
{
emxArray_real_T *emx;
int32_T size[2];
int32_T numEl;
int32_T i;
size[0] = rows;
size[1] = cols;
emxInit_real_T(&emx, 2);
numEl = 1;
for (i = 0; i < 2; i++) {
numEl *= size[i];
emx->size[i] = size[i];
}
emx->data = data;
emx->numDimensions = 2;
emx->allocatedSize = numEl;
emx->canFreeData = FALSE;
return emx;
}
我目前正试图在C#中调用它,如下所示:
[DllImport(@"C:blabla.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern emxArray_real_T emxCreateWrapper_real_T(double[,] data, int rows, int cols);
double[,] array2D = new double[,] { { 1 }, { 3 }, { 5 }, { 7 } };
var x = emxCreateWrapper_real_T(array2D, 1, 4);
但是得到:
Method's type signature is not PInvoke compatible.
emxArray_real_T
当前如下所示:
[StructLayout(LayoutKind.Sequential)]
public struct emxArray_real_T
{
//public IntPtr data;
//public IntPtr size;
double[] data;
int[] size;
public int allocatedSize;
public int numDimensions;
[MarshalAs(UnmanagedType.U1)]
public bool canFreeData;
}
存在多个问题。首先,C++函数返回一个指针(emxArray_real_T*),但导入声明返回一个结构。这是行不通的。此外,您在导入声明中将数据声明为double[,],但在结构中声明为double[]。建议:
- 用类替换结构
- 确定数据应该是双精度[]还是双精度[,]
- 还要检查real_T的最终大小。我相信它是一个依赖于平台的变量,可以是浮点(32位)或双精度(64位)
相关文章:
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 字符类型转换不兼容
- Qt:如何使不兼容的发送方/接收方参数兼容?
- 视觉工作室 2017;启用 /permissive 时,类型 "const wchar_t *" 的参数与类型 "PWSTR" 的参数不兼容
- 使用不兼容的分配器复制分配无序列图
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- 该对象具有与成员函数不兼容的类型限定符.为什么会出现此错误?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 为什么范围算法与 std 的迭代器不兼容?
- Winpcap Findalldevs const char * 与 char * 不兼容
- C++ 类型的参数与 void (__cdecl*)(void) 类型的参数不兼容,当调用 std::atexit()
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 如何在C++中停止调用不兼容的方法?
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- 函数范围的静态变量如何导致与共享库中函数代码的未来使用不兼容
- 如何修复"方法的类型与 PInvoke 不兼容"
- C++不兼容的迭代器类型
- 编译器错误"在if语句中分配不兼容的类型"
- C#调用C 第三方DLL(无源)提出异常 - 不兼容PinVoke
- PInvoke - 方法的类型签名与 PInvoke 不兼容