How do I properly return a char * from an Unmanaged DLL to C#?

后端 未结 5 842
生来不讨喜
生来不讨喜 2021-01-06 08:57

Function signature:

char * errMessage(int err);

My code:

[DllImport(\"api.dll\")]       
internal static extern char[] errMessage(int err);         


        
5条回答
  •  再見小時候
    2021-01-06 09:29

    try this:

    [DllImport("api.dll")]
    [return : MarshalAs(UnmanagedType.LPStr)]
    internal static extern string errMessage(int err);
    ...
    string message = errMessage(err);
    

    I believe C# is smart enough to handle the pointer and return you a string.

    Edit: Added the MarshalAs attribute

提交回复
热议问题