C++方法
BOOL ReadStrValue(UINT vm_handle,LPCSTR pKey, unsigned short Offset, LPSTR pVal);
C#调用方式如下
[DllImport("Test.dll", CharSet = CharSet.Ansi, EntryPoint = "ReadStrValue", CallingConvention = CallingConvention.StdCall)]
public static extern bool ReadStrValue(uint handle, [MarshalAs(UnmanagedType.LPStr)] string pKey, ushort offset, [MarshalAs(UnmanagedType.LPStr)] StringBuilder val);
这里StringBuilder类型本身在c#里面传递的就是引用地址,所以不需要加ref或者out关键字。
C++方法
BOOL ReadNumValue(UINT vm_handle,LPCSTR pKey, unsigned short Offset, PDWORD pVal);
C#调用方法如下
[DllImport("test.dll", CharSet = CharSet.Ansi, EntryPoint = "ReadNumValue", CallingConvention = CallingConvention.StdCall)]
public static extern bool ReadNumValue(uint handle, [MarshalAs(UnmanagedType.LPStr)] string pKey, ushort offset, ref int val);
PDWORD是指向DWORD的指针类型,DWORD是双WORD类型,每个WORD占两个字节,因此DWORD占四个字节,和Int类型对应