3

I want to convert struct object into byte[] and vice versa in windows phone 7 application I have used this code it works in windows form application perfect but it is not working in windows phone application and error is

'System.Runtime.InteropServices.Marshal' does not contain a definition for 'AllocHGlobal'   

'System.Runtime.InteropServices.Marshal' does not contain a definition for 'FreeHGlobal'

my code is

public static byte[] getBytes(object o)
        {
            int size = Marshal.SizeOf(o);
            byte[] arr = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(o, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);
            return arr;
        }

        public static object getStruct(byte[] arr, object o)
        {            
            int size = Marshal.SizeOf(o);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.Copy(arr, 0, ptr, size);
            o = (object)Marshal.PtrToStructure(ptr, o.GetType());
            Marshal.FreeHGlobal(ptr);
            return o;
        }    
matzone
  • 5,703
  • 3
  • 17
  • 20
Kevan
  • 107
  • 7

0 Answers0