您的位置:程序门 -> .net技术 -> c#



(高分求助)byte[] 转化为结构时候出错


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


(高分求助)byte[] 转化为结构时候出错
发表于:2007-03-16 17:18:10 楼主
using   system;
using   system.collections.generic;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.text;
using   system.windows.forms;
using   system.runtime.interopservices;
using   system.runtime.serialization;
using   system.io;
using   system.runtime.serialization.formatters.binary;


namespace   rtu_center

{        
             

          public   struct   rec
          {
                  public   string   head;
                  public   int   x,   y;
          }
        public   partial   class   form1   :   form
        {
                public   form1()
                {
                        initializecomponent();
                }
                         
                private   void   listview1_selectedindexchanged(object   sender,   eventargs   e)
                {

                }
                private   void   display(string   ss)
                {
                        textbox3.appendtext( "\n ");
                        textbox3.appendtext(ss);
                }

                public   delegate   void   mydelegate(string   astr);
                public   unsafe   static   byte[]   struct2bytes(object   obj)
                {
                        int   size   =   marshal.sizeof(obj);
                        byte[]   bytes   =   new   byte[size];
                        fixed   (byte*   pb   =   &bytes[0])
                        {
                                intptr   ipt   =   new   intptr(pb);
                                marshal.structuretoptr(obj,   ipt,   true);
                                //marshal.freehglobal(ipt);
                        }
                       
                        return   bytes;
                }
                public   unsafe   static   object   bytes2struct(byte[]   b,object   r)
                {
                        fixed   (byte*   pb   =   &b[0])
                        {
                                r   =   marshal.ptrtostructure(new   intptr(pb),   r.gettype());                        
                          //程序运行到此处第一次不会出去,第二次运行时候错误为:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。                                    
                        }
                        return   r;
                       
                }
               

                private   void   serialport1_datareceived(object   sender,   system.io.ports.serialdatareceivedeventargs   e)
                {
                        byte[]   b=new   byte[12];
                        serialport1.read(b,   0,   12);
                        rec   r   =   new   rec();
                        r=(rec)bytes2struct(b,(object)   r);
                        textbox3.invoke(new   mydelegate(display),   new   object[]   {   r.head   });
                }
             

                private   void   button3_click(object   sender,   eventargs   e)
                {
                        rec   r   =   new   rec();
                        r.head   =   "$wad ";
                        r.x   =   10;
                        r.y   =   14;
                        byte[]   b   =   new   byte[marshal.sizeof(r)];
                        b=struct2bytes(r);
                        serialport1.write(b,   0,   marshal.sizeof(r));
                       
                }
本人比较所有byte[]转化为结构的代码,没有一个合适可用,不知道哪位高人有!
发表于:2007-03-16 17:19:231楼 得分:0
在线等高人回答。。。。。。。。。。。。。。
发表于:2007-03-16 17:28:212楼 得分:0
在线等高人回答。。。。。。。。。。。。。。
发表于:2007-03-16 17:52:173楼 得分:0
在线等高人回答。。。。。。。。。。。。。。
发表于:2007-03-19 16:33:104楼 得分:0
rec   是哪个using下面的??
发表于:2007-03-19 16:36:065楼 得分:0
结构定义要加
[structlayout....]属性


快速检索

最新资讯
热门点击