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);
user8469759
  • 2,522
  • 6
  • 26
  • 50
  • The C# side should be `IntPtr` then pass `byte[]` to it. Read the duplicate for more information. – Programmer Apr 13 '18 at 10:25
  • Is the C++ fine then? – user8469759 Apr 13 '18 at 10:41
  • Use `unsigned char*` on the C++ side instead of `char*` – Programmer Apr 13 '18 at 10:44
  • What does the`fixed` keyword do? I don't understand that construct, is that like a casting? – user8469759 Apr 13 '18 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 – Programmer Apr 13 '18 at 10:49
  • @Programmer Got you, I've changed the code, but it doesn't make any difference. – user8469759 Apr 13 '18 at 11:07
  • Notice how I made the C++ call from the `fixed` block not outside of it. You haven't done that. I realized you are passing many arrays, so just make it easier for you and `GCHandle.Alloc` to pin the array. Forget about the `fixed` keyword. For example, `GCHandle pinHandle = GCHandle.Alloc(strA, GCHandleType.Pinned);` then use `pinHandle.AddrOfPinnedObject();` to get the address of the array and pass it to the C++ side. – Programmer Apr 13 '18 at 11:25
  • See [this](https://stackoverflow.com/questions/49482647/convert-opencv-mat-to-texture2d/49486419#49486419) post for full example. You have to pin each array. While this many not solve the problem, it will prevent awful ones you'll run into in the future. Note that you have to show in your question what you are passing to the C++ side and how that array is initialized. You also have to show how you discovered you are getting `A\0B\0C\0`. Create a small test array, pass to C++ and then show us how it is initialized and how you got your result. – Programmer Apr 13 '18 at 11:28

0 Answers0