Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. How can I convert a Class to byte[] then back to Class for Windows Mobile? [modified]

How can I convert a Class to byte[] then back to Class for Windows Mobile? [modified]

Scheduled Pinned Locked Moved Mobile
questionhelpsysadmindata-structuressecurity
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kiotaya
    wrote on last edited by
    #1

    I have a class that I need to convert to a byte array for a Windows Mobile device. The byte array is sent using ssl to a server, which needs to rebuild the class. I have used BinaryFormatter on a PC client that worked great. However I can't use that with the compact framework. This is the code I am currently trying to use but its giving me the error message "Type 'PackageTrans' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.".

    public byte[] ClassToBytearray(object obj)
    {
    int Length = Marshal.SizeOf(obj); <----------This is where I get the error.
    byte[] bytearray = new byte[Length];
    IntPtr ptr = Marshal.AllocHGlobal(Length);
    Marshal.StructureToPtr(obj, ptr, false);
    Marshal.Copy(ptr, bytearray, 0, Length);
    Marshal.FreeHGlobal(ptr);
    return bytearray;
    }

    This is my first time using Marshal. Why wouldn't a meaningful size or offset be computed? What is it that I am missing? Is there a different way I should be doing this? Any and all help would be greatly appreciated. Thanks -Kio

    modified on Friday, June 11, 2010 1:25 AM

    S 1 2 Replies Last reply
    0
    • K Kiotaya

      I have a class that I need to convert to a byte array for a Windows Mobile device. The byte array is sent using ssl to a server, which needs to rebuild the class. I have used BinaryFormatter on a PC client that worked great. However I can't use that with the compact framework. This is the code I am currently trying to use but its giving me the error message "Type 'PackageTrans' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.".

      public byte[] ClassToBytearray(object obj)
      {
      int Length = Marshal.SizeOf(obj); <----------This is where I get the error.
      byte[] bytearray = new byte[Length];
      IntPtr ptr = Marshal.AllocHGlobal(Length);
      Marshal.StructureToPtr(obj, ptr, false);
      Marshal.Copy(ptr, bytearray, 0, Length);
      Marshal.FreeHGlobal(ptr);
      return bytearray;
      }

      This is my first time using Marshal. Why wouldn't a meaningful size or offset be computed? What is it that I am missing? Is there a different way I should be doing this? Any and all help would be greatly appreciated. Thanks -Kio

      modified on Friday, June 11, 2010 1:25 AM

      S Offline
      S Offline
      Sir Dot Net
      wrote on last edited by
      #2

      Look into serialization, there are many options -> Binary, xml, soap, etc.

      1 Reply Last reply
      0
      • K Kiotaya

        I have a class that I need to convert to a byte array for a Windows Mobile device. The byte array is sent using ssl to a server, which needs to rebuild the class. I have used BinaryFormatter on a PC client that worked great. However I can't use that with the compact framework. This is the code I am currently trying to use but its giving me the error message "Type 'PackageTrans' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.".

        public byte[] ClassToBytearray(object obj)
        {
        int Length = Marshal.SizeOf(obj); <----------This is where I get the error.
        byte[] bytearray = new byte[Length];
        IntPtr ptr = Marshal.AllocHGlobal(Length);
        Marshal.StructureToPtr(obj, ptr, false);
        Marshal.Copy(ptr, bytearray, 0, Length);
        Marshal.FreeHGlobal(ptr);
        return bytearray;
        }

        This is my first time using Marshal. Why wouldn't a meaningful size or offset be computed? What is it that I am missing? Is there a different way I should be doing this? Any and all help would be greatly appreciated. Thanks -Kio

        modified on Friday, June 11, 2010 1:25 AM

        1 Offline
        1 Offline
        1eyhk1
        wrote on last edited by
        #3

        hello How did you manage?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups