0

I'm building a plugin for Unity and looked at this answer for a method for reading byte data from C++ to C#: Passing byte array from C++ unmanaged dll to C# unity

C++ code:

char DAQ_API* Read()
    {
         char Buffer[30];
         PopulateArray(Buffer);
         return Buffer;
    }
int DAQ_API FreeMemory(char* arrayPtr) 
    {
        if (arrayPtr != nullptr) {
            delete[] arrayPtr;
        }
        return 0;
    }

C# code:

[DllImport("DAQ")]
private static extern IntPtr Read();
[DllImport("DAQ")]
public static extern int FreeMemory(IntPtr ptr);

In the update loop:

IntPtr ptr = Read();
byte[] packet = new byte[30];
Marshal.Copy(ptr, packet, 0, 30);
FreeMemory(ptr);

The size of the byte array is 30, and the first 8 bytes are correct, and the rest looks like junk data:

0 240 0 89 86 23 7 9 240 67 189 31 0 0 0 0 96 211 95 0 0 0 0 0 224 210 95 0 0 0 

What's happening that's causing the bad data for the rest of the array?

karamazovbros
  • 950
  • 1
  • 11
  • 40

0 Answers0