为不同版本的 Windows 构建驱动程序

如果要为不同版本的 Windows 编写驱动程序,请参阅以下部分,以下部分提供了一些有关如何使用 Windows 驱动程序工具包 (WDK) 8.1 或 WDK 8、Visual Studio 和 MSBuild 构建这些驱动程序的指南。

适用于构建用户模式和内核模式驱动程序的指南

  • 使用 WDK 提供的目标配置和平台来构建驱动程序。始终使用支持目标 Windows 版本的最新版本 WDK。例如,要为 Windows XP 构建驱动程序,你必须使用 Windows 7 WDK。 但如果为 Windows 8.1、Windows 8 或 Windows 7 构建驱动程序,则需使用 WDK 8.1 和 Visual Studio。
  • 如果你的驱动程序必须在单个版本的 Windows 上运行,请为与目标 Windows 版本相匹配的目标配置和平台构建驱动程序。例如,如果构建仅在 WDK 8.1 上运行的驱动程序,请在配置管理器中指定 Win8.1
  • 如果希望驱动程序在多个版本的 Windows 上运行但不具备仅在较新版本中可用的功能,则请为希望驱动程序支持的最旧版本构建驱动程序。例如,如果你的驱动程序只能在 Windows Vista 及更高版本的 Windows 上运行,并且只能使用 Windows Vista 上提供的功能,请为项目配置指定 Vista

适用于构建内核模式驱动程序的指南

  • 如果希望在多个版本的 Windows 上运行内核模式驱动程序并动态确定该驱动程序可用的功能,请使用最新版本操作系统的构建配置来构建驱动程序。例如,如果希望驱动程序支持 Windows 7 及所有更高版本的 Windows,但想要驱动程序在 Windows 8.1 或更高版本的操作系统上运行时可使用最初在 Windows 8.1 中可用的某些功能,请指定 Windows 8.1 (Win8.1) 作为目标配置。

  • 使用 RtlIsNtDdiVersionAvailableRtlIsServicePackVersionInstalled 函数可确定运行时驱动程序可用的 Windows 版本。有关详细信息,请参阅为不同版本的 Windows 编写驱动程序

  • 为指向驱动程序必须按条件调用的函数的指针创建原型

  • 如果你有 WDM 驱动程序或非 KMDF 内核模式驱动程序,并且目标是用在 Windows 8.1 或 Windows 8 上,但又希望能在早期版本的 Windows 上运行这些驱动程序,那么还需要替代链接器 $(KernelBufferOverflowLib) 选项。在选择 Windows 8 或 Windows 8.1 配置时,驱动程序会与早期 Windows 版本中不可用的 BufferOverflowFastFailK.lib 链接。 对于 Windows 7 和 Vista,则必须与 BufferOverflowK.lib 链接。

    有两种方式可重写 $(KernelBufferOverflowLib) 链接器选项,一种是使用 MSBuild,另一种是使用 Visual Studio。

    使用 MSBuild:

    msbuild /p:KernelBufferOverflowLib="C:\Program Files (x86)\Windows Kits\8.1\Lib\win8\km\x64\BufferOverflowK.lib" /p:platform=x64 /p:Configuration="Win8 Release" myDriver.sln
    

    使用 Visual Studio:

    使用记事本或其他文本编辑器打开驱动程序项目文件 (*.vcxproj)。在项目文件中,找到你的驱动程序支持的配置的 <PropertyGroup>,并添加以下行以覆盖默认链接选项:

    
       <KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
    

    例如,如果你的驱动程序支持 Windows 8.1 和 Windows 8 调试和发布版本,则这些配置部分应如下所示:

      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Win8.1 Debug|Win32'" Label="Configuration">
        <TargetVersion>WindowsV6.3</TargetVersion>
        <UseDebugLibraries>true</UseDebugLibraries>
        <KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
        <PlatformToolset>WindowsKernelModeDriver8.1</PlatformToolset>
        <ConfigurationType>Driver</ConfigurationType>
        <DriverType>KMDF</DriverType>
      </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|Win32'" Label="Configuration">
        <TargetVersion>WindowsV6.3</TargetVersion>
        <UseDebugLibraries>false</UseDebugLibraries>
        <KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
        <PlatformToolset>WindowsKernelModeDriver8.1</PlatformToolset>
        <ConfigurationType>Driver</ConfigurationType>
        <DriverType>KMDF</DriverType>
      </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Win8 Debug|Win32'" Label="Configuration">
        <TargetVersion>Windows8</TargetVersion>
        <UseDebugLibraries>true</UseDebugLibraries>
        <KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
        <PlatformToolset>WindowsKernelModeDriver8.1</PlatformToolset>
        <ConfigurationType>Driver</ConfigurationType>
        <DriverType>KMDF</DriverType>
      </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Win8 Release|Win32'" Label="Configuration">
        <TargetVersion>Windows8</TargetVersion>
        <UseDebugLibraries>false</UseDebugLibraries>
        <KernelBufferOverflowLib>$(DDK_LIB_PATH)\BufferOverflowK.lib</KernelBufferOverflowLib>
        <PlatformToolset>WindowsKernelModeDriver8.1</PlatformToolset>
        <ConfigurationType>Driver</ConfigurationType>
        <DriverType>KMDF</DriverType>
      </PropertyGroup>
    

    <KernelBufferOverflowLib> 元素必须显示在驱动程序项目文件中导入 Microsoft.Cpp.props 的元素之前,该元素用于导入工具集。

    在修改并保存驱动程序项目文件之后,即可在 Visual Studio 中打开项目文件并构建驱动程序。

相关主题

为不同版本的 Windows 编写驱动程序

构建驱动程序