Integration problems between C++ and C#

Asked

Viewed 65 times

1

Hello, I’m having problems with the integration between a DLL in C++ with C# from Unity. I found some codes (https://stackoverflow.com/questions/27390856/cant-marshal-array-of-stucts-from-c-to-c-sharp-in-unity)

This is my C++ code, in it I theoretically send a 2 point structure to Unity in C#.

struct Pontos {
int i;
float f;
};
Pontos **pontos;
DllExport bool SetPoints(Pontos *** a, int *i)
{
pontos = new Pontos*[4];
for (int j = 0; j < 4; j++) {
        pontos[j] = new Pontos; // Actually create each object.
        pontos[j]->i = j;
        pontos[j]->f = (float)j;
    }
    *a = pontos;
    *i = 4;
    return true;
}

Here the Code in C#, where I try to receive the points I send and save. In Unity where the script is added the following message appears "No Monobehaviour scripts in the file, or their Names do not match the file name", I don’t know what that means.

[DllImport("dll")]
 private static extern bool SetPoints(out IntPtr ptrResultVerts, out int resultVertLength);
public struct Pontos
{
int i;
float f;
};     
void Start()
{ 
IntPtr ptrNativeData = IntPtr.Zero;
int itemsLength = 0;

bool success = SetPoints(out ptrNativeData, out itemsLength);
if (!success)
{
    return;
}

Pontos[] SArray = new Pontos[itemsLength];  // Where the final data will be stored.
IntPtr[] SPointers = new IntPtr[itemsLength];

Debug.Log("Length: " + itemsLength); // Works!
Marshal.Copy(ptrNativeData, SPointers, 0, itemsLength); // Seems not to work.

for (int i = 0; i < itemsLength; i++)
{
    Debug.Log("Pointer: " + SPointers[i]);
    SArray[i] = (Pontos)Marshal.PtrToStructure(SPointers[i], typeof(Pontos));
}

If anyone knows what’s wrong please help me. Thanks.

  • This second code, in C#, should not be an inherited class of MonoBehavior? Did you just copy an excerpt of code, or is the script all right? If it is all like this (that is, without the definition of class and inheritance), I think the error is quite obvious: it is not a valid Unity script.

  • It’s just an excerpt from the code, in the statement I inherit the Monobehavior

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.