创建驱动程序项目时,可以指定最低目标操作系统,这是驱动程序可运行的最低 Windows 版本。 例如,可以指定Windows 7是最低目标操作系统。 在这种情况下,驱动程序将在 Windows 7 及更高版本的 Windows 上运行。
Note
如果为特定最低版本的 Windows 开发驱动程序,并且希望驱动程序在更高版本的 Windows 上运行,则不得使用任何未记录的函数,并且不得以文档所述方式之外的任何方式使用已记录的函数。 否则,驱动程序可能无法在更高版本的 Windows 上运行。 即使已小心地仅使用有文档说明的函数,在每次发布 Windows 新版本时,也应在新版 Windows 上测试驱动程序。
仅使用常用功能编写多版本驱动程序
在设计将在多个版本的 Windows 上运行的驱动程序时,最简单的方法是允许驱动程序仅使用该驱动程序将运行的所有 Windows 版本共有的 DDI 函数和结构。 在这种情况下,请将最低目标操作系统设置为驱动程序将支持的最早版本的 Windows。
例如,若要支持所有版本的Windows,从Windows 7开始,应:
设计和实现驱动程序,使其仅使用Windows 7中存在的这些功能。
构建驱动程序时,请将 Windows 7 指定为最低目标操作系统。
虽然此过程很简单,但它可能会限制驱动程序仅使用更高版本的 Windows 上提供的功能子集。 在许多情况下,当操作系统功能可用时,需要利用较新的操作系统功能来提高安全性、提高可靠性或启用较新的功能。
编写使用版本依赖功能的多版本驱动程序
内核模式驱动程序可以动态确定操作系统提供的 API 是否可用,或者驱动程序正在运行的Windows版本,并选择使用该运行时环境中可用的功能。 例如,必须支持 Windows 所有版本(从 Windows 7 开始)的驱动程序,可以在运行时确定其正在其上运行的 Windows 版本。 如果驱动程序在Windows 7上运行,则它只能使用Windows 7支持的 DDI 函数。 但是,同一个驱动程序可以使用 Windows 8 独有的其他 DDI 函数,例如,当其运行时检查确定这些 API 当前存在,或者确定它在 Windows 8 上运行时。
Note
建议尽可能检查功能或 API 可用性,而不是尝试检查驱动程序是否在特定操作系统版本或更高版本上运行。
有条件地调用依赖于 Windows 版本的函数
内核模式驱动程序可以使用 MmGetSystemRoutineAddress 或 MmGetSystemRoutineAddressEx 函数动态检查特定 API 在当前运行时环境中是否可用。 这些函数还返回驱动程序可用于调用 API 的函数指针。
Note
为了帮助保留类型检查并防止意外错误,应创建镜像原始函数类型的 typedef。
示例:确定 API 可用性并有条件地调用 API
typedef
NTSTATUS
(*PFN_IoOpenDriverRegistryKey)(
PDRIVER_OBJECT DriverObject,
DRIVER_REGKEY_TYPE RegKeyType,
ACCESS_MASK DesiredAccess,
ULONG Flags,
PHANDLE DriverRegKey
);
VOID ExampleFunction(VOID) {
NTSTATUS status = STATUS_UNSUCCESSFUL;
HANDLE persistentStateKey = NULL;
PFN_IoOpenDriverRegistryKey pfnIoOpenDriverRegistryKey = NULL;
UNICODE_STRING functionName = {0};
RtlInitUnicodeString(&functionName, L"IoOpenDriverRegistryKey");
pfnIoOpenDriverRegistryKey = (PFN_IoOpenDriverRegistryKey)MmGetSystemRoutineAddress(&functionName);
if (pfnIoOpenDriverRegistryKey != NULL) {
// Open a key to where state can be stored under the driver service
status = pfnIoOpenDriverRegistryKey(g_GlobalStructure.DriverObject,
DriverRegKeyPersistentState,
KEY_WRITE,
0,
&persistentStateKey);
} else {
// Fall back to opening up a different location to store state in
}
// Use the opened registry key
}
确定Windows版本
内核模式驱动程序可以使用 RtlVerifyVersionInfo 函数动态检查当前正在运行的Windows版本。
Note
建议尽可能检查功能或 API 可用性,而不是尝试检查驱动程序是否在特定操作系统版本或更高版本上运行。
示例:确定Windows版本
以下示例检测当前运行的操作系统版本是否大于或等于版本 10.0,并检测内部版本号是否大于或等于内部版本 22000(Windows 11版本 21H2)。
...
NTSTATUS Status = STATUS_SUCCESS;
RTL_OSVERSIONINFOEXW VersionInfo = {0};
ULONG TypeMask = 0;
ULONGLONG ConditionMask = 0;
VersionInfo.dwOSVersionInfoSize = sizeof(VersionInfo);
VersionInfo.dwMajorVersion = 10;
VersionInfo.dwMinorVersion = 0;
VersionInfo.dwBuildNumber = 22000;
TypeMask = VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER;
VER_SET_CONDITION(ConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(ConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(ConditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);
Status = RtlVerifyVersionInfo(&VersionInfo,
TypeMask,
ConditionMask);
if (NT_SUCCESS(Status)) {
//
// The call to RtlVerifyVersionInfo succeeded, so the running OS
// version and build number is greater than or equal to the value
// specified. Do appropriate action for newer OS versions.
//
} else if (Status == STATUS_REVISION_MISMATCH) {
//
// The running OS version is less than the value specified. Do
// appropriate action for older OS versions.
//
} else {
//
// There was an error comparing to the running OS version. Do
// appropriate action for when the OS version is not known.
//
}
...