工作中使用平台调用总结

1、托管代码向非托管代码传递字符串

非托管API:

extern "C" __declspec(dllexport)

int _cdeclWriteDataToFCSFile(char* FilePath);

托管:

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

public extern static bool WriteFCS( string FilePath, string SampleLabel); 

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( ref shortresponseCode, out IntPtrparameters, ref intnSize); 

//需要将数据从非托管内存指针复制到托管字符数组,关键代码如下:

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+= ",";

    }

  }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值