问题描述
C++的原型函数:CRTEXPORTCOURT_RECORD_RESCALLBACKCourtRecordGetKeyValue(COURT_PARAM_KEYkey,void*data);typedefenumCOURT_PARAM_KEY{KEY_MERGE_MAP,KEY_OUTPUT_MAP};data是根据不同的key来传入不同的结构体数组;现我用C#调用如下写法:[DllImport("CourtRecord.dll")]publicstaticexternCOURT_RECORD_RESCourtRecordSetKeyValue(COURT_PARAM_KEYkey,IntPtrdata);定义结构体[StructLayout(LayoutKind.Sequential)]publicstructOUTPUT_MAP_PARAM{publicintnInternalChannel;publicintnOutStreamPos;};#region//TODO:读取数据库设置合成关系//CourtRecordSetKeyValueList<Tuple<int,string>>merge=DataAccess.Biz.Screen.GetScreenInfo(CourtID);MERGE_MAP_PARAM[]mergepara=newMERGE_MAP_PARAM[6];for(inti=0;i<merge.Count;i++){mergepara[i].nInternalChannel=merge[i].Item1;mergepara[i].nMergePos=Convert.ToInt32(merge[i].Item2);}IntPtr[]ptArr1=newIntPtr[1];ptArr1[0]=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MERGE_MAP_PARAM))*6);IntPtrpt1=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MERGE_MAP_PARAM)));Marshal.Copy(ptArr1,0,pt1,1);CourtRecordSetKeyValue(COURT_PARAM_KEY.KEY_MERGE_MAP,pt1);//调用方法#endregion可是这样调用方法不会报错,但是结构体的值却改变了。