问题描述
C++:有这样一个结构:typedefstruct_Event{BYTECode;//eventcodeBYTEDotID;//controllercomponentIDWORDreserved;//reservedSYSTEMTIMETime;//eventtime_Event*pNext;//pointernextevent;}TEvent,*PEvent;函数:intNT_GetEvent(unsignedlongLineID,BYTECtrlID,BYTE*Bagid,BYTE*EventCount,TEvent*lpEvent);
我现在想在C#中调用这样的结构,我该怎么写啊,麻烦大家指教指教#¥,下面是我自己写的,但有问题,只能读到且只能读一条记录(如设备有1个事件,可以正常使用,但如果有2个或2个以上事件就读不出来)[StructLayout(LayoutKind.Sequential)]//[SerializableAttribute]//[ComVisibleAttribute(true)]unsafepublicstructPEvent{publicbyteCode;publicbyteDotID;publicushortreserved;publicTimeTime;publicIntPtrpNext;}函数:[DllImport("TC3XY.dll")]publicstaticexternintNT_GetEvent(ushortLineID,shortCtrlID,refshortBagid,refshortEventCount,refPEventlpEvent);//获取设备事件
解决方案
解决方案二:
呵呵,遇到过对于动态库无语了找下我找下忘记了,再发上来
解决方案三:
最后一个不是指针么用Marshal.PtrToStructure(IntPtr,Type)将内存拷贝到一个结构体里面,就可以了.
解决方案四:
找不到了!不好意思!网上去找哈!
解决方案五:
引用2楼akirya的回复:
最后一个不是指针么用Marshal.PtrToStructure(IntPtr,Type)将内存拷贝到一个结构体里面,就可以了.
我也想到这个方法了,但没用成功,我把我的代码贴出来,麻烦您帮我看看:我的结构:[StructLayout(LayoutKind.Sequential)]publicstructPEvent{publicbyteCode;publicbyteDotID;publicTCardNumberCardNumber;publicushortreserved;publicTimeTime;publicIntPtrpNext;}publicvoidGetEvent(){byteBagid=this.SEventBagId;byteEventCount=this.SEventCount;intrs=0;stringStr="";inti=0;EkcoTog.PEventEvent=newEkcoTog.PEvent();EkcoTog.PEventlpNext=newEkcoTog.PEvent();try{Bagid=SEventBagId;EventCount=SEventCount;//MessageBox.Show("success");rs=EkcoTog.DPU3001_Dll.NT_GetEvent(LineID,CtrlID,refBagid,refEventCount,refEvent);this.listBox1.Items.Add(rs.ToString());if(rs==0){SEventBagId=Bagid;//SaveeventpackagenumberfornexttimeSEventCount=EventCount;//SaveeventcountfornexttimeEkcoTog.PEvent[]Sevent=newPEvent[EventCount];stringstr="BagID:"+Bagid.ToString()+"EventCount:"+EventCount.ToString();this.listBox1.Items.Add(str);lpNext=Event;for(i=0;i<EventCount;i++){intm=i+1;Str+=m.ToString()+"DateTime:"+lpNext.Time.wMonth.ToString()+"-"+lpNext.Time.wDayOfWeek.ToString()+"-"+lpNext.Time.wHour.ToString();Str+=""+lpNext.Time.wMinute.ToString()+":"+lpNext.Time.wSecond.ToString()+":"+lpNext.Time.wMilliseconds.ToString();Str+="EventCode:"+lpNext.Code.ToString();Str+="ComponentAddress:"+lpNext.DotID.ToString();Str+="CardOffset:"+lpNext.CardNumber.dwCardNo.ToString()+"THISISEND!"+lpNext.pNext.ToString();this.listBox1.Items.Add(Str);//lpNext.pNext=Event.pNext;//lpNext=ptr;//lpNext=lpNext.pNext.ToPointer();//IntPtrp=lpNext.pNext;//lpNext.pNext=IntPtr.Zero;IntPtrptr=Marshal.ReadIntPtr(Event,i);Sevent[i]=(PEvent)Marshal.PtrToStructure(Event.pNext,typeof(PEvent));return;}}if(rs==133){this.listBox1.Items.Add("所有事件都已经上载,没有新事件!");}else{this.listBox1.Items.Add("获取事件失败!"+rs.ToString());}}catch{//this.listBox1.Items.Add("WW!");}}
解决方案六:
引用3楼tigerleq的回复:
找不到了!不好意思!网上去找哈!
谢谢关注!
解决方案七:
我也想到这个方法了,但没用成功,我把我的代码贴出来,麻烦您帮我看看:
解决方案八:
Marshal.PtrToStructure(Event.pNext,Event);
解决方案九:
能不能具体点啊Marshal.PtrToStructure(Event.pNext,Event);