C++ C# char* string 传出的char* StringBuilder short short char byte char[n] fixed byte[n] 结构指针 结构指针 函数指针 委托 结构体数组使用IntPtr
参考,C#中调用WindowsAPI GetVersionEx() //申明结构在内存中布局 [StructLayout(LayoutKind.Sequential)] public struct struct_OsVersionInformation { public int dwOSVersionInfoSize; public int dwMajorVersion; public int dwMinorVersion; public int dwBuildNumber; public int dwPlatformID; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] //资料来源:学网(www.xue5.com),原文地址:http://www.xue5.com/itedu/200802/109538.html public string szCSDVersion; public ushort wServicePackMajor; public ushort wServicePackMinor; public ushort wSuiteMask; public byte wProductType; public byte wReserved; }//API函数调用申明 [DllImport("Kernel32.dll", EntryPoint = "GetVersionEx private static extern bool GetVersionEx2(ref struct_OsVersionInformation pStruct_VersionInformation);//注意ref 引用关键字 public static void GetOSVersion(ref struct_OsVersionInformation struct_osvi) { //建立结构实例 struct_osvi = new struct_OsVersionInformation(); //为新建的结构分配内存 struct_osvi.dwOSVersionInfoSize = Marshal.SizeOf(struct_osvi); //结构引用 if (!GetVersionEx2(ref struct_osvi)) { throw new Exception("GetVersionEx调用失败"); } }
char* string
传出的char* StringBuilder
short short
char byte
char[n] fixed byte[n]
结构指针 结构指针
函数指针 委托
结构体数组使用IntPtr
[StructLayout(LayoutKind.Sequential)]
public struct struct_OsVersionInformation
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
//资料来源:学网(www.xue5.com),原文地址:http://www.xue5.com/itedu/200802/109538.html
public string szCSDVersion;
public ushort wServicePackMajor;
public ushort wServicePackMinor;
public ushort wSuiteMask;
public byte wProductType;
public byte wReserved;
}//API函数调用申明
[DllImport("Kernel32.dll", EntryPoint = "GetVersionEx
private static extern bool GetVersionEx2(ref struct_OsVersionInformation pStruct_VersionInformation);//注意ref 引用关键字
public static void GetOSVersion(ref struct_OsVersionInformation struct_osvi)
{
//建立结构实例
struct_osvi = new struct_OsVersionInformation();
//为新建的结构分配内存
struct_osvi.dwOSVersionInfoSize = Marshal.SizeOf(struct_osvi);
//结构引用
if (!GetVersionEx2(ref struct_osvi))
{
throw new Exception("GetVersionEx调用失败");
} }
private static extern bool GetVersionEx2(ref struct_OsVersionInformation pStruct_VersionInformation);
[dllimport("your.dll"), Charset=ANSI]
的这种格式. 自己百度下.
有些C++生产的dll字符集设置问题也会引起这种现象