Unity,将bytes数组传递给本机插件中的C ++函数

时间:2018-04-13 10:22:22

标签: c# c++ unity3d

我在C ++中有以下签名

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

在Unity方面我有

[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
        );

然而,当一个来自unity的字符串(比如ABC)被传递给C ++时,我会得到像A\0B\0C\0这样的字符串。有办法解决这个问题吗?

谢谢。

更新:完成@Programmer建议,没有任何区别。这可能是因为我可能会使用这些功能吗?

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 ++签名:

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);

0 个答案:

没有答案