0

I've the following signature in C++

bool myFunc(
        char* strA,
        char* strB,
        char* strC,
        char* strD,
        char* strE,
        int lengthA,
        int lengthB,
        int lengthC,
        int lengthD,
        int lengthF);

In Unity side I have

[DllImport("myDLL")]
    public static extern bool myFunc(
        char[] strA,
        char[] strB,
        char[] strC,
        char[] strD,
        char[] strE,
        int lengthA,
        int lengthB,
        int lengthC,
        int lengthD,
        int lengthE
        );

However when a string from unity, say ABC, is passed to the C++ I get something like A\0B\0C\0. Is there a way to fix this?

Thank you.

Update: Done as @Programmer suggested, doesn't make any difference. Might this be due to how I'm maybe using those functions?

C# :

[DllImport("myDLL")]
static extern bool myFunc(
    IntPtr strA,
    IntPtr strB,
    IntPtr strC,
    IntPtr strD,
    IntPtr strE,
    int lengthA,
    int lengthB,
    int lengthC,
    int lengthD,
    int lengthE
    );

public unsafe bool myFunc(
    byte[] strA,
    byte[] strB,
    byte[] strC,
    byte[] strD,
    byte[] strE,
    int lengthA,
    int lengthB,
    int lengthC,
    int lengthD,
    int lengthE
    )
{

    IntPtr p1;
    IntPtr p2;
    IntPtr p3;
    IntPtr p4;
    IntPtr p5;

    fixed (byte* p = strA)
    {
        p1 = (IntPtr)p;
    }

    fixed (byte* p = strB)
    {
        p2 = (IntPtr)p;
    }

    fixed (byte* p = strC)
    {
        p3 = (IntPtr)p;
    }

    fixed (byte* p = strD)
    {
        p4 = (IntPtr)p;
    }

    fixed (byte* p = strE)
    {
        p5 = (IntPtr)p;
    }

    myFunc(
        p1, p2, p3, p4, p5, 
        lengthA, lengthB, lengthC, lengthD, lengthE);

    return true;

}

C++ signature:

bool myFunc(
        unsigned char* strA,
        unsigned char* strB,
        unsigned char* strC,
        unsigned char* strD,
        unsigned char* strE,
        int lengthA,
        int lengthB,
        int lengthC,
        int lengthD,
        int lengthE);
8
  • The C# side should be IntPtr then pass byte[] to it. Read the duplicate for more information. Commented Apr 13, 2018 at 10:25
  • Is the C++ fine then? Commented Apr 13, 2018 at 10:41
  • Use unsigned char* on the C++ side instead of char* Commented Apr 13, 2018 at 10:44
  • What does thefixed keyword do? I don't understand that construct, is that like a casting? Commented Apr 13, 2018 at 10:46
  • I explained this in the duplicate See #2. You can also Google ("C# fixed keyword") it if that explanation is not clear to you. It even has a comment above of what it does above it in the code section which you can also google if you don't understand it Commented Apr 13, 2018 at 10:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.