OSInfo 示例
该示例说明如何将两种对象作为参数传递给需要具有嵌入字符缓冲区的结构的非托管函数,一种对象是通过值传递的格式化类,一种对象是通过引用传递的结构。
OSInfo 示例使用以下非托管函数(这里同时显示其原始函数声明):
从 Kernel32.dll 导出的 GetVersionEx。
// BOOL GetVersionEx(LPOSVERSIONINFO lpVersionInfo);
传递到该函数的初始结构包含以下元素:
typedef struct _OSVERSIONINFO
{
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[ 128 ];
} OSVERSIONINFO;
在该示例中,当传递给非托管函数时,OSVersionInfo 类和 OSVersionInfo2 结构将产生相同的结果。 MarshalAsAttribute 特性将 UnmanagedType 枚举值设置为 ByValTStr,后者用于标识在非托管结构中出现的内联的、固定长度的字符数组。
LibWrap 类包含两个原型:GetVersionEx 将该类作为参数传递,GetVersionEx2 将结构作为参数传递。 通过显式应用 InAttribute 和 OutAttribute 特性,该示例确保将 OSVersionInfo 作为 In/Out 参数进行封送处理并且调用方可以查看封送回的更改。 (为了提高性能,类的默认方向特性为 In,以防止调用方查看封送回的更改。)
通常通过值传递的 OSVersionInfo2 结构使用 ref(在 Visual Basic 中为 ByRef)关键字声明并通过引用传递。 Marshal.SizeOf 方法确定非托管结构的大小(以字节为单位)。
声明原型
<StructLayout(LayoutKind.Sequential)> _
Public Class OSVersionInfo
Public OSVersionInfoSize As Integer
Public MajorVersion As Integer
Public MinorVersion As Integer
Public BuildNumber As Integer
Public PlatformId As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public CSDVersion As String
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Structure OSVersionInfo2
Public OSVersionInfoSize As Integer
Public MajorVersion As Integer
Public MinorVersion As Integer
Public BuildNumber As Integer
Public PlatformId As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public CSDVersion As String
End Structure
Public Class LibWrap
Declare Ansi Function GetVersionEx Lib "kernel32" Alias _
"GetVersionExA" ( <[In], Out> ByVal osvi As OSVersionInfo ) As Boolean
Declare Ansi Function GetVersionEx2 Lib "kernel32" Alias _
"GetVersionExA" ( ByRef osvi As OSVersionInfo2 ) As Boolean
End Class
[StructLayout(LayoutKind.Sequential)]
public class OSVersionInfo
{
public int OSVersionInfoSize;
public int MajorVersion;
public int MinorVersion;
public int BuildNumber;
public int PlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public String CSDVersion;
}
[StructLayout(LayoutKind.Sequential)]
public struct OSVersionInfo2
{
public int OSVersionInfoSize;
public int MajorVersion;
public int MinorVersion;
public int BuildNumber;
public int PlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public String CSDVersion;
}
public class LibWrap
{
[DllImport("kernel32")]
public static extern bool GetVersionEx([In, Out] OSVersionInfo osvi);
[DllImport("kernel32", EntryPoint="GetVersionEx")]
public static extern bool GetVersionEx2(ref OSVersionInfo2 osvi);
}
[StructLayout(LayoutKind::Sequential)]
public ref class OSVersionInfo
{
public:
int OSVersionInfoSize;
int MajorVersion;
int MinorVersion;
int BuildNumber;
int PlatformId;
[MarshalAs(UnmanagedType::ByValTStr, SizeConst=128)]
String^ CSDVersion;
};
[StructLayout(LayoutKind::Sequential)]
public value struct OSVersionInfo2
{
public:
int OSVersionInfoSize;
int MajorVersion;
int MinorVersion;
int BuildNumber;
int PlatformId;
[MarshalAs(UnmanagedType::ByValTStr, SizeConst=128)]
String^ CSDVersion;
};
public ref class LibWrap
{
public:
[DllImport("kernel32")]
static bool GetVersionEx([In, Out] OSVersionInfo^ osvi);
[DllImport("kernel32", EntryPoint="GetVersionEx")]
static bool GetVersionEx2(OSVersionInfo2% osvi);
};
调用函数
Public Class App
Public Shared Sub Main()
Console.WriteLine(vbNewLine + "Passing OSVersionInfo as a class")
Dim osvi As New OSVersionInfo()
osvi.OSVersionInfoSize = Marshal.SizeOf(osvi)
LibWrap.GetVersionEx(osvi)
Console.WriteLine("Class size: {0}", osvi.OSVersionInfoSize)
Console.WriteLine("OS Version: {0}.{1}", osvi.MajorVersion, osvi.MinorVersion)
Console.WriteLine(vbNewLine + "Passing OSVersionInfo2 as a struct")
Dim osvi2 As new OSVersionInfo2()
osvi2.OSVersionInfoSize = Marshal.SizeOf(osvi2)
LibWrap.GetVersionEx2(osvi2)
Console.WriteLine("Struct size: {0}", osvi2.OSVersionInfoSize)
Console.WriteLine("OS Version: {0}.{1}", osvi2.MajorVersion, osvi2.MinorVersion)
End Sub
End Class
public class App
{
public static void Main()
{
Console.WriteLine("\nPassing OSVersionInfo as a class");
OSVersionInfo osvi = new OSVersionInfo();
osvi.OSVersionInfoSize = Marshal.SizeOf(osvi);
LibWrap.GetVersionEx(osvi);
Console.WriteLine("Class size: {0}", osvi.OSVersionInfoSize);
Console.WriteLine("OS Version: {0}.{1}", osvi.MajorVersion, osvi.MinorVersion);
Console.WriteLine("\nPassing OSVersionInfo as a struct" );
OSVersionInfo2 osvi2 = new OSVersionInfo2();
osvi2.OSVersionInfoSize = Marshal.SizeOf(osvi2);
LibWrap.GetVersionEx2(ref osvi2);
Console.WriteLine("Struct size: {0}", osvi2.OSVersionInfoSize);
Console.WriteLine("OS Version: {0}.{1}", osvi2.MajorVersion, osvi2.MinorVersion);
}
}
public ref class App
{
public:
static void Main()
{
Console::WriteLine("\nPassing OSVersionInfo as a class");
OSVersionInfo^ osvi = gcnew OSVersionInfo();
osvi->OSVersionInfoSize = Marshal::SizeOf(osvi);
LibWrap::GetVersionEx(osvi);
Console::WriteLine("Class size: {0}", osvi->OSVersionInfoSize);
Console::WriteLine("OS Version: {0}.{1}", osvi->MajorVersion, osvi->MinorVersion);
Console::WriteLine("\nPassing OSVersionInfo as a struct" );
OSVersionInfo2 osvi2;
osvi2.OSVersionInfoSize = Marshal::SizeOf(osvi2);
LibWrap::GetVersionEx2(osvi2);
Console::WriteLine("Struct size: {0}", osvi2.OSVersionInfoSize);
Console::WriteLine("OS Version: {0}.{1}", osvi2.MajorVersion, osvi2.MinorVersion);
}
};
请参见
概念
其他资源
Creating Prototypes in Managed Code