最近在做一个小项目,想通过手机和pc机进行通讯,想传输结构体,在网上找到了一个Marshal.AllocHGlobal(size);这么一个方法,但是该方法不能用于手机,所以请高手给个手机上可以用的方法(.net ,Windows mobile)

解决方案 »

  1.   

    刚才看了下MSDN,上面说Marshal类支持的平台有那么多:
    Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360这里面包含你要的手机的吧。
      

  2.   

    手机倒是没有试过转成byte[]  Marshal.AllocHGlobal(size)只是给分配了一个内存空间
    还需要Marshal.Copy()从内存中拷贝到byte[] lz最好贴出具体错误
      

  3.   

    Marshal.AllocHGlobal(size)只是给分配了一个内存空间
    这个方法在手机里不支持(msdn上面写了),所以求教还有别的方法吗?
      

  4.   

    用unsafe代码定义结构体。
      

  5.   

    Marshal.PtrToStructure
    Marshal.StructureToPtr
    这两个方法 转换结构体
      

  6.   

    int size = Marshal.SizeOf(structObj);
                //创建byte数组
                byte[] bytes = new byte[size];
                //分配结构体大小的内存空间
                IntPtr structPtr = Marshal.AllocHGlobal(size);
                //将结构体拷到分配好的内存空间
                Marshal.StructureToPtr(structObj, structPtr, false);
                //从内存空间拷到byte数组
                Marshal.Copy(structPtr, bytes, 0, size);
                //释放内存空间
                Marshal.FreeHGlobal(structPtr);
    手机里面不支持(红色标记的)那个方法,该怎么解决?
      

  7.   

      SendBytes _Bytes =new SendBytes();
                _Bytes.a=100;
                _Bytes.b=200;            int _StructSize =Marshal.SizeOf(_Bytes);
                
                IntPtr _StructIntPtr = Marshal.AllocCoTaskMem(_StructSize);
                Marshal.StructureToPtr(_Bytes, _StructIntPtr, false);
                byte[] _ValueBytes = new byte[_StructSize];
                Marshal.Copy(_StructIntPtr, _ValueBytes, 0, _StructSize);
            
                           IntPtr _ObjectIntPtr = Marshal.AllocCoTaskMem(_StructSize);
                Marshal.Copy(_ValueBytes, 0, _ObjectIntPtr, _StructSize);
                SendBytes _Object = (SendBytes)Marshal.PtrToStructure(_ObjectIntPtr, typeof(SendBytes));
        public struct SendBytes
            {
                public int a;            public ushort b;
            }