您的位置:首页 > 其它

工作中使用平台调用总结

2015-03-14 12:50 253 查看
1、托管代码向非托管代码传递字符串

非托管API:

extern "C" __declspec(dllexport)

int _cdeclWriteDataToFCSFile(char* FilePath);

托管:

[DllImport(_Business.dll, EntryPoint = "WriteDataToFCSFile",
CallingConvention = CallingConvention.Cdecl)]

publicexternstaticbool
WriteFCS(string FilePath,stringSampleLabel);

2、托管代码向非托管代码发送数组
非托管API:
extern "C" __declspec(dllexport)

int
_cdecl SendCommand(short ComCode,shortDstCode,short SrcCode,floatParam[],int nSize);

托管:

[DllImport(_dllName,EntryPoint ="SendCommand",CallingConvention =CallingConvention.Cdecl)]
public static
extern int SendCommand(short CmdCode,short DstDeviceCode,shortSrcDeviceCode,float[] Params,int
nSize);

3、非托管代码通过参数向托管代码传递一般类型值和数组
非托管API:
extern
"C"__declspec(dllexport)
int
_cdeclGetHardDeviceInfo(short* Order,float** OrderContent,int*nSize);//参数1返回短整型值,参数2返回浮点型数组,参数3返回整形值,该值表示参数2数组大小

托管:

[DllImport(_dllName,EntryPoint ="GetHardDeviceInfo",CallingConvention=CallingConvention.Cdecl)]
public static
extern boolGetResponse(refshortresponseCode,outIntPtrparameters,refintnSize);

//需要将数据从非托管内存指针复制到托管字符数组,关键代码如下:
if (nSize >= 0)
{
float[] paramsArray =
new float[nSize];
Marshal.Copy(parametersPtr,paramsArray, 0, nSize);

}

4、非托管代码通过返回值想托管代码传递数组
非托管API:

extern "C"__declspec(dllexport)

int* _cdecl GetTheArray(int*nSize);

托管:

[DllImport(_dllName,EntryPoint ="GetTheArray",CallingConvention =CallingConvention.Cdecl)]
unsafe publicstaticexternint* GetTheArray(ref int nSize);
使用方法举例:
unsafe
{
int* theArrayPtr;
string arrayMember =
"";
int int= 0;
theArrayPtr= BusinessCenterDLL.GetTheArray(ref nSize);
if(theArrayPtr!=
null)
{
for (int i = 0; i< nSize; i++)
{
originalCommand+= theArrayPtr[i].ToString();
originalCommand+= ",";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: