Marshal::Copy fails when trying to copy from a char* to a System::Char[]
-
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? -
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?FYI
System::Char
is an Unicode character that corresponds to native C++'swchar_t
, andSystem::Byte
corresponds to native C++'schar
."We make a living by what we get, we make a life by what we give." --Winston Churchill
-
FYI
System::Char
is an Unicode character that corresponds to native C++'swchar_t
, andSystem::Byte
corresponds to native C++'schar
."We make a living by what we get, we make a life by what we give." --Winston Churchill
-
I guess I was doing the right thing, then. I just thought there would be a signed/unsigned mismatch. Thanks.
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: