1

I receive IntPtr value from C++ library method and i need to get byte[] array from this received IntPtr. When I try do this:

byte[] myArray = new byte[Marshal.SizeOf(myReceivedIntPtr)];
Marshal.Copy(myReceivedIntPtr, myArray, 0, Marshal.SizeOf(myReceivedIntPtr));

I receive an exception: AccessViolationException. What I missed?

EDIT:

Here is Method name and params in header of C++ library:

int MSR_DecodeTrack(char *AscBuff, unsigned char *BinBuff, unsigned char bpc, unsigned char parity, unsigned char ss, unsigned char es);

And I call it from C# this way:

 UInt32 result;
 IntPtr myReceivedIntPtr;
 IntPtr BinBuff;
 byte BPC1 = 7, Parity1 = 1, SS1 = 0x05, ES1 = 0x1F;

 result = MSR_DecodeTrack(ref myReceivedIntPtr, ref BinBuff, BPC1, Parity1, SS1, ES1);

And here is else code:

 byte[] myArray = new byte[Marshal.SizeOf(myReceivedIntPtr)];
 Marshal.Copy(myReceivedIntPtr, myArray, 0, Marshal.SizeOf(myReceivedIntPtr));
Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109
  • 1
    Do none of the other answers on google help? http://www.google.co.uk/search?q=Marshal.Copy+AccessViolationException – Ash Burlaczenko Nov 23 '11 at 10:06
  • 2
    what is the type of Tk1AscBuff? ... Marshal.SizeOf(myReceivedIntPtr) will return the size of an IntPtr, usually 4/8 Byte ... do you plan to copy one pointer from the unmanaged memory into the managed array? – DarkSquirrel42 Nov 23 '11 at 10:11
  • Tk1AscBuff is renamed myReceivedIntPtr, I've renaimed it already. So how to get size of byte array using IntPtr? – Timur Mustafaev Nov 23 '11 at 10:13
  • You cant get the size of the byte array using teh intptr unless the array somehow contains that data. I suspect it doesnt. You need to show us the c++ calls you use to get the intptr - they probably return the size of the array – Russell Troywest Nov 23 '11 at 10:16

2 Answers2

2

I'm pretty sure Marshal.SizeOf(myReceivedIntPtr) will return the size of the IntPtr object, not the size of the unmanaged array. You will need to get the size of the unmanaged array from the c++ library.

I haven't had to do this in a while so I'm a bit rusty but from looking at the c++ headers I think it is expecting a pointer to a buffer. You are giving it a pointer to a pointer (intptr is a pointer and passing it by ref means you are passing the c++ code a pointer to that pointer). Firstly, don't use ref. Secondly, I suspect you are supposed to be passing it an already sized buffer - is there any documentation describing what you are supposed to pass to the function? What is the return code from the function? You are checking it for success aren't you? ;)

Russell Troywest
  • 8,635
  • 3
  • 35
  • 40
  • Could you tell me how to do this? – Timur Mustafaev Nov 23 '11 at 10:11
  • Include the code you are calling to get the intptr to the unmanaged array in the first place and I may be able to help. I don't think c++ arrays have bounds information so you need to keep track of the size of the array yourself. The method you call to get the pointer probably returns the size as well – Russell Troywest Nov 23 '11 at 10:13
0

The method named MSR_DecodeTrack should also return the length of each array it allocates.

As far as I know, when you allocate an array in C/C++ environment, you get a pointer to the zero element of that array, nothing more. For that reason, you should also somehow return the length of the allocated array back to the caller. It can be done by an additional "ref" parameter.

HTH.

Ron Klein
  • 9,178
  • 9
  • 55
  • 88