C#调用C++ dll时参数传递编译不通过,该怎么处理
C#调用C++ dll时参数传递编译不通过
各位大神,我按如下方法调用C++ dll的导出函数,编译时总是提示调用参数无数,请问可能是什么问题呢?
C++函数原型:
int Plat_GetAllControlCell(
int iUserHandle,
int iNeedGetNum,
LPPLAT_CONTROLCELLINFO pCellBuffer,
int* pOutputNum);
其中LPPLAT_CONTROLCELLINFO是结构指针。
我声明的C#对应函数为:
[DllImport("PlatformSDK.dll", EntryPoint = "Plat_GetAllControlCell", CallingConvention = CallingConvention.Cdecl)]
public static extern int Plat_GetAllControlCell(
int iUserHandle,
int iNeedGetNum,
ref PLAT_CONTROLCELLINFO pCellBuffer,
ref int pOutputNum
);
我的调用方法:
int PlatRtnCell = 0;
PlatInterface.Plat_GetAllControlCell(g_iLoginHandle, 0, null, ref PlatRtnCell); //g_iLoginHandle是int类型的成员变量
非常感谢您的帮助!
------解决思路----------------------
结构体传空不行,因为在函数中要赋值
1. 把 LPPLAT_CONTROLCELLINFO 结构体的转换贴出来看看。
2.可以把 CallingConvention = CallingConvention.Cdecl改成CallingConvention.Stdcall再试试
------解决思路----------------------
------解决思路----------------------
没仔细看工具转换出来滴
------解决思路----------------------
接口中 ref PLAT_CONTROLCELLINFO pCellBuffer 把ref去掉
------解决思路----------------------
各位大神,我按如下方法调用C++ dll的导出函数,编译时总是提示调用参数无数,请问可能是什么问题呢?
C++函数原型:
int Plat_GetAllControlCell(
int iUserHandle,
int iNeedGetNum,
LPPLAT_CONTROLCELLINFO pCellBuffer,
int* pOutputNum);
其中LPPLAT_CONTROLCELLINFO是结构指针。
我声明的C#对应函数为:
[DllImport("PlatformSDK.dll", EntryPoint = "Plat_GetAllControlCell", CallingConvention = CallingConvention.Cdecl)]
public static extern int Plat_GetAllControlCell(
int iUserHandle,
int iNeedGetNum,
ref PLAT_CONTROLCELLINFO pCellBuffer,
ref int pOutputNum
);
我的调用方法:
int PlatRtnCell = 0;
PlatInterface.Plat_GetAllControlCell(g_iLoginHandle, 0, null, ref PlatRtnCell); //g_iLoginHandle是int类型的成员变量
非常感谢您的帮助!
------解决思路----------------------
结构体传空不行,因为在函数中要赋值
1. 把 LPPLAT_CONTROLCELLINFO 结构体的转换贴出来看看。
2.可以把 CallingConvention = CallingConvention.Cdecl改成CallingConvention.Stdcall再试试
------解决思路----------------------
PLAT_CONTROLCELLINFO pCellBuffer = new PLAT_CONTROLCELLINFO();new个结构体了再传
int PlatRtnCell = 0;
PlatInterface.Plat_GetAllControlCell(g_iLoginHandle, 0, pCellBuffer, ref PlatRtnCell);
------解决思路----------------------
没仔细看工具转换出来滴
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct PLAT_CONTROLCELLINFO {
/// int
public int iControlCellID;
/// int
public int iParentCellID;
/// char[128]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=128)]
public string csControlCellName;
}
public partial class NativeMethods {
/// Return Type: int
///iUserHandle: int
///iNeedGetNum: int
///pCellBuffer: LPPLAT_CONTROLCELLINFO->_tagControlCellInfo*
///pOutputNum: int*
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="Plat_GetAllControlCell")]
public static extern int Plat_GetAllControlCell(int iUserHandle, int iNeedGetNum, ref PLAT_CONTROLCELLINFO pCellBuffer, ref int pOutputNum) ;
}
------解决思路----------------------
接口中 ref PLAT_CONTROLCELLINFO pCellBuffer 把ref去掉
------解决思路----------------------
public static extern int Plat_GetAllControlCell(int iUserHandle, int iNeedGetNum, [Out, MarshalAs(LPArray, SizeParamIndex=1)] PLAT_CONTROLCELLINFO pCellBuffer[], [Out]ref int pOutputNum) ;我更加觉得 C++ 参数 的 Buffer 是期望一个数组。