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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Error in StructureToPtr

Error in StructureToPtr

Scheduled Pinned Locked Moved Managed C++/CLI
hardwaredata-structuresregexhelp
5 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.
  • J Offline
    J Offline
    javad_2005
    wrote on last edited by
    #1

    [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] ref struct MyStruct { [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )] array^data; }; /////////////////////// array ^Serialize(MyStruct ^Struct) { int len=Marshal::SizeOf(Struct); IntPtr ptr=Marshal::AllocHGlobal(len); array^ rawdatas = gcnew array(len); try { Marshal::StructureToPtr(Struct,ptr,false); Marshal::Copy( ptr, rawdatas, 0, len ); Marshal::FreeHGlobal( ptr ); ////////////////////////////////////////////////////////////////// return rawdatas; } catch(ArgumentException ^e) { MessageBox::Show(e->Message); } return rawdatas; } ////////////// Error in StructureToPtr: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

    M 1 Reply Last reply
    0
    • J javad_2005

      [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] ref struct MyStruct { [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )] array^data; }; /////////////////////// array ^Serialize(MyStruct ^Struct) { int len=Marshal::SizeOf(Struct); IntPtr ptr=Marshal::AllocHGlobal(len); array^ rawdatas = gcnew array(len); try { Marshal::StructureToPtr(Struct,ptr,false); Marshal::Copy( ptr, rawdatas, 0, len ); Marshal::FreeHGlobal( ptr ); ////////////////////////////////////////////////////////////////// return rawdatas; } catch(ArgumentException ^e) { MessageBox::Show(e->Message); } return rawdatas; } ////////////// Error in StructureToPtr: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Please format your posted code in a readable and compilable  fashion!! Use the pre tags and fix the <> brackets! Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        Please format your posted code in a readable and compilable  fashion!! Use the pre tags and fix the <> brackets! Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        javad_2005
        wrote on last edited by
        #3

        [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] ref struct MyStruct { [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )] array<Byte>^data; }; /////////////////////// array<Byte> ^Serialize(MyStruct ^Struct) { int len=Marshal::SizeOf(Struct); IntPtr ptr=Marshal::AllocHGlobal(len); array<Byte>^ rawdatas = gcnew array(len); try { Marshal::StructureToPtr(Struct,ptr,false); Marshal::Copy( ptr, rawdatas, 0, len ); Marshal::FreeHGlobal( ptr ); ////////////////////////////////////////////////////////////////// return rawdatas; } catch(ArgumentException ^e) { MessageBox::Show(e->Message); } return rawdatas; } ////////////// Error in StructureToPtr: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

        L M 2 Replies Last reply
        0
        • J javad_2005

          [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] ref struct MyStruct { [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )] array<Byte>^data; }; /////////////////////// array<Byte> ^Serialize(MyStruct ^Struct) { int len=Marshal::SizeOf(Struct); IntPtr ptr=Marshal::AllocHGlobal(len); array<Byte>^ rawdatas = gcnew array(len); try { Marshal::StructureToPtr(Struct,ptr,false); Marshal::Copy( ptr, rawdatas, 0, len ); Marshal::FreeHGlobal( ptr ); ////////////////////////////////////////////////////////////////// return rawdatas; } catch(ArgumentException ^e) { MessageBox::Show(e->Message); } return rawdatas; } ////////////// Error in StructureToPtr: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          If you add < PRE> tags as Mark suggested, I might actually look at your code... They tend to preserve indentation, making things much easier to read. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          1 Reply Last reply
          0
          • J javad_2005

            [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] ref struct MyStruct { [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )] array<Byte>^data; }; /////////////////////// array<Byte> ^Serialize(MyStruct ^Struct) { int len=Marshal::SizeOf(Struct); IntPtr ptr=Marshal::AllocHGlobal(len); array<Byte>^ rawdatas = gcnew array(len); try { Marshal::StructureToPtr(Struct,ptr,false); Marshal::Copy( ptr, rawdatas, 0, len ); Marshal::FreeHGlobal( ptr ); ////////////////////////////////////////////////////////////////// return rawdatas; } catch(ArgumentException ^e) { MessageBox::Show(e->Message); } return rawdatas; } ////////////// Error in StructureToPtr: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            I don't have any idea how you're actually using this code, but from the error message, is the number of elements in the data array == 1024 as specified by SizeConst? Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            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