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. General Programming
  3. Managed C++/CLI
  4. Marshal::Copy fails when trying to copy from a char* to a System::Char[]

Marshal::Copy fails when trying to copy from a char* to a System::Char[]

Scheduled Pinned Locked Moved Managed C++/CLI
debugginghelpquestion
4 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.
  • C Offline
    C Offline
    Cyrilix
    wrote on last edited by
    #1

    I have some code that fails when I do this:

    Char mgData[] = new Char[numBytes];
    Marshal::Copy(data, mgData, 0, numBytes);

    ...but does not fail when I do this:

    Byte mgData[] = new Byte[numBytes];
    Marshal::Copy(data, mgData, 0, numBytes);

    data is of type char*, and contains binary data in the form of individual bytes (not represented as text at all). numBytes is the length of char*, when it was created with...

    char* data = new char[numBytes]

    The specific error that I'm getting is in Marshal::Copy, and the debugger points me to memcpy.asm. This leads me to believe that it's something unconventional, or that I'm simply not handling my unmanaged to managed conversions properly. There's a bit of background though... the reason why I'm trying to convert from unmanaged char* to managed Byte[] is because a BinaryWriter object won't properly write binary data when it's in the form of char*. If I were to call... BinaryWriter.Write(char*), it would simply write until it thinks that it's at the end of a text string (likely a null-terminated character, or something along those lines). Furthermore, I'm not sure if using the Byte instead of the Char is incorrect, since Byte is an unsigned type, and Char (I think) is signed, which matches char* (also signed). Any suggestions?

    G 1 Reply Last reply
    0
    • C Cyrilix

      I have some code that fails when I do this:

      Char mgData[] = new Char[numBytes];
      Marshal::Copy(data, mgData, 0, numBytes);

      ...but does not fail when I do this:

      Byte mgData[] = new Byte[numBytes];
      Marshal::Copy(data, mgData, 0, numBytes);

      data is of type char*, and contains binary data in the form of individual bytes (not represented as text at all). numBytes is the length of char*, when it was created with...

      char* data = new char[numBytes]

      The specific error that I'm getting is in Marshal::Copy, and the debugger points me to memcpy.asm. This leads me to believe that it's something unconventional, or that I'm simply not handling my unmanaged to managed conversions properly. There's a bit of background though... the reason why I'm trying to convert from unmanaged char* to managed Byte[] is because a BinaryWriter object won't properly write binary data when it's in the form of char*. If I were to call... BinaryWriter.Write(char*), it would simply write until it thinks that it's at the end of a text string (likely a null-terminated character, or something along those lines). Furthermore, I'm not sure if using the Byte instead of the Char is incorrect, since Byte is an unsigned type, and Char (I think) is signed, which matches char* (also signed). Any suggestions?

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      FYI System::Char is an Unicode character that corresponds to native C++'s wchar_t, and System::Byte corresponds to native C++'s char.

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      C 1 Reply Last reply
      0
      • G George L Jackson

        FYI System::Char is an Unicode character that corresponds to native C++'s wchar_t, and System::Byte corresponds to native C++'s char.

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        C Offline
        C Offline
        Cyrilix
        wrote on last edited by
        #3

        I guess I was doing the right thing, then. I just thought there would be a signed/unsigned mismatch. Thanks.

        M 1 Reply Last reply
        0
        • C Cyrilix

          I guess I was doing the right thing, then. I just thought there would be a signed/unsigned mismatch. Thanks.

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

          Cyrilix wrote:

          I just thought there would be a signed/unsigned mismatch.

          Nah. Marshal.Copy() has no idea what your passed IntPtr points to. You just have to make sure there's enough room there for whatever gets copied :) 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