共用方式為


為不同版本的 Windows 建置驅動程式

如果您要 撰寫不同版本的 Windows 驅動程式,下一節提供一些指導方針,說明如何使用 Windows 驅動程式套件 (WDK) 、Visual Studio 和 MSBuild 建置這些驅動程式。

適用于建置使用者模式和核心模式驅動程式的指導方針

  • 使用 WDK 提供的目標群組態和平臺建置驅動程式。 請一律使用最新版的 WDK,以支援您想要鎖定的 Windows 版本。 如需 WDK 和作業系統版本支援的相關資訊,請參閱 安裝 Windows 驅動程式套件的預覽版本下載 Windows 驅動程式套件
  • 如果您的驅動程式只能在單一版本的 Windows 上執行,請針對符合目標 Windows 版本的目標群組態和平臺建置驅動程式。
  • 如果您想要讓驅動程式在多個版本的 Windows 上執行,但若沒有僅適用于較新版本的功能,請針對您想要驅動程式支援的最舊版本建置驅動程式。

如果您要以 Windows 7、Windows 8或Windows 8.1為目標,請使用 Configuration Manager或手動在 .vcxproj 檔案中設定TargetVersion,例如 <TargetVersion>Windows7</TargetVersion>

如果您要將目標設為目標Windows 10或Windows 11,請同時設定TargetVersion_NT_TARGET_VERSION,例如 <TargetVersion>Windows10</TargetVersion> <_NT_TARGET_VERSION>0xA000006</_NT_TARGET_VERSION>

_NT_TARGET_VERSION 值會列在格式 NTDDI_WIN10_* 為 的 Sdkddkver.h 標頭檔中,例如 #define NTDDI_WIN10_RS5 0x0A000006

適用于建置核心模式驅動程式的指導方針

  • 如果您想要讓核心模式驅動程式在多個版本的 Windows 上執行,並動態判斷驅動程式可用的功能,請使用最新版本作業系統的組建組態來建置驅動程式。 例如,如果您想要讓驅動程式支援從 Windows 8.1 開始的所有 Windows 版本,但當您的驅動程式在作業系統的 Windows 10 或更新版本上執行時,若要使用Windows 10中第一個可用的特定功能,請將 Windows 10 (Win10) 指定為目標群組態。

  • 使用 RtlIsNtDdiVersionAvailableRtlIsServicePackVersionInstalled 函式來判斷驅動程式在執行時間可用的 Windows 版本。 如需詳細資訊,請參閱 撰寫不同 Windows 版本的驅動程式

  • 建立驅動程式必須有條件呼叫之函式指標的原型。

  • 如果您有 WDM 驅動程式或非 KMDF 核心模式驅動程式,而且您是以Windows 8.1或Windows 8為目標,但也想要在舊版 Windows 上執行,您必須覆寫linker $ (KernelBufferOverflowLib) 選項。 當您選取Windows 8或Windows 8.1組態時,驅動程式會與 BufferOverflowFastFailK.lib 連結,這在舊版 Windows 中無法使用。 針對 Windows 7 和 Vista,您必須改為與 BufferOverflowK.lib 連結。

    有兩種方式可以使用 MSBuild 或 Visual Studio 覆寫 $ (KernelBufferOverflowLib) 連結器選項。

    使用 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 >,然後新增下列這一行以覆寫預設連結器選項:

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

    例如,如果您的驅動程式支援Windows 8.1和Windows 8偵錯和發行組建,這些組態區段看起來會如下所示:

    XML
      <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 中開啟專案檔並建置驅動程式。