char *作为Unity插件的函数返回值

时间:2013-10-15 08:58:06

标签: plugins char unity3d

我想使用char *作为返回值(就像在Unity的示例插件中一样)但是我得到了一些无效的标记(效汬o)而不是简单的" Hello" 。什么想法可能是错的?

DLL:

extern "C"
{
    const __declspec(dllexport) char*  PrintHello()
    {
        return "Hello";
    }
}

统一:

public class audiotest : MonoBehaviour 
{
    [DllImport("ASimplePlugin")]
    private static extern IntPtr PrintHello();

    void Start ()
    {
        Debug.Log(Marshal.PtrToStringAuto(PrintHello()));
    }
}

可以找到Unity的示例插件http://docs.unity3d.com/Documentation/Images/manual/SimplestPluginExample-4.0.zip

我正在使用Unity 4.2.1f4和Visual Studio 2010

1 个答案:

答案 0 :(得分:3)

使用PtrToStringAnsi代替PtrToStringAuto。

您的DLL返回ANSI字符串,但PtrToStringAuto在除Windows 98之外的任何内容上都需要Unicode。

相关问题