在C#中,如何调用返回包含字符串指针的非托管结构的DLL函数?

Eri*_*ric 3 c# interop

我得到了一个DLL("InfoLookup.dll"),它在内部分配结构并从查找函数返回指向它们的指针.结构包含字符串指针:

extern "C"
{
   struct Info
   {
      int id;
      char* szName;
   };

   Info* LookupInfo( int id );
}
Run Code Online (Sandbox Code Playgroud)

在C#中,如何声明结构布局,声明Interop调用,以及(假设返回非空值)使用字符串值?换句话说,我如何将以下内容翻译成C#?

#include "InfoLookup.h"
void foo()
{
   Info* info = LookupInfo( 0 );
   if( info != 0 && info->szName != 0 )
      DoSomethingWith( info->szName );
   // NOTE: no cleanup here, the DLL is caching the lookup table internally
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 5

尝试以下布局.使用PInvoke Interop Assistant自动生成代码.手动编码的LookpInfoWrapper()

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Info {

    /// int
    public int id;

    /// char*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
    public string szName;
}

public partial class NativeMethods {

    /// Return Type: Info*
    ///id: int
    [System.Runtime.InteropServices.DllImportAttribute("InfoLookup.dll", EntryPoint="LookupInfo")]
public static extern  System.IntPtr LookupInfo(int id) ;

    public static LoopInfoWrapper(int id) {
       IntPtr ptr = LookupInfo(id);
       return (Info)(Marshal.PtrToStructure(ptr, typeof(Info));
    }

}
Run Code Online (Sandbox Code Playgroud)