為不同版本的 Windows 編寫驅動程式

來源:互聯網
上載者:User

標籤:保留   cti   microsoft   允許   type   子集   文檔   abs   and   

MSDN原文:https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff554887(v=vs.85).aspx

建立驅動程式項目時,指定基本的目標作業系統,該系統是運行驅動程式的基本版本的 Windows。例如,你可以指定 Windows 7 為基本的目標作業系統。在這種情況下,驅動程式會在 Windows 7 和更高版本的 Windows 上運行。

注意  如果為特定的基本版本的 Windows 開發驅動程式且希望驅動程式運行在更高版本的 Windows 上,則不得使用任何未記錄的函數,也不得採用文檔中介紹方法之外的任何方法使用記錄的函數。否則,驅動程式可能會無法運行在更高版本的 Windows 上。即便你已經很小心,僅使用記錄的函數,也應在每次發布新版本的 Windows 時在其上對驅動程式進行測試。 編寫僅使用共同功能的多版本驅動程式

設計將在多個版本的 Windows 上啟動並執行驅動程式時,最簡單的方法是允許驅動程式僅使用運行該驅動程式的所有版本的 Windows 共同的 DDI 函數和結構。在此情形下,將最基本的目標作業系統設定為驅動程式支援的最早版本的 Windows。

例如,要支援從 Windows 7 開始的所有版本的 Windows,應執行以下操作:

  1. 設計和實現驅動程式,以便該驅動程式僅使用 Windows 7 中提供的那些功能。

  2. 產生驅動程式時,將 Windows 7 指定為最基本的目標作業系統。

雖然此過程很簡單,但它可能會限制驅動程式僅使用更高版本的 Windows 上提供的功能子集。

編寫使用版本相關功能的多版本驅動程式

核心模式驅動程式可以動態確定它啟動並執行是哪個版本的 Windows 並且選擇使用該版本上提供的功能。例如,必須支援從 Windows 7 開始的 Windows 所有版本的驅動程式可以在運行時確定它所啟動並執行 Windows 版本。 如果驅動程式在 Windows 7 上運行,則它只能使用 Windows 7 支援的 DDI 函數。但是,同一個驅動程式可以使用僅適用於 Windows 8 的其他 DDI 函數,例如,當其運行時檢查功能確定它在 Windows 8 上運行時。

確定 Windows 版本

RtlIsNtDdiVersionAvailable 為驅動程式可以在運行時用於確定特定版本的 Windows 提供的功能是否可用的函數。該函數的原型如下所示:

  
BOOLEAN RtlIsNtDdiVersionAvailable(IN ULONG Version)

在此原型中,Version 是一個值,指示 Windows DDI 的所需版本。此值必須為 DDI 版本常量之一,在 sdkddkver.h 中定義,例如 NTDDI_WIN8 或 NTDDI_WIN7。

當調用程式運行在 Version. 指定的相同的 Windows 版本或更高版本上時,RtlIsNtDdiVersionAvailable 會返回 TRUE。

驅動程式還可以通過調用 RtlIsServicePackVersionInstalled 函數檢查特定的 Service Pack。該函數的原型如下所示:

  
BOOLEAN RtlIsServicePackVersionInstalled(IN ULONG Version)

在此原型中,Version 為一個值,指示所需的 Windows 版本和 Service Pack。此值必須為 DDI 版本常量之一,在 sdkddkver.h 中定義,例如 NTDDI_WS08SP3。

注意,僅當作業系統版本與指定版本完全符合時,RtlIsServicePackVersionInstalled 才會返回 TRUE。因此,如果驅動程式不是在 Windows Server 2008 SP4 上運行,則調用將 Version 設定為 NTDDI_WS08SP3 的 RtlIsServicePackVersionInstalled 會失敗。

條件性調用與 Windows 版本相關的函數

在驅動程式確定電腦上可用的特定作業系統版本後,該驅動程式可以使用 MmGetSystemRoutineAddress 函數動態尋找常式並通過指標調用該常式。Windows 7 和更高版本的作業系統版本上提供了此函數。

注意  若要協助保留鍵入檢查和防止出現無意識的錯誤,應建立映射原始函數類型的 typedef。 樣本:確定 Windows 版本並條件性調用版本相關的函數

此程式碼範例(來自驅動程式的標頭檔)將 PAISQSL 類型定義為指向 KeAcquireInStackQueuedSpinLock 函數的指標。然後,該樣本聲明此類型的 AcquireInStackQueuedSpinLock 變數。

  
... //// Pointer to the ordered spin lock function.// This function is only available on Windows 7 and// later systems typedef (* PAISQSL) (KeAcquireInStackQueuedSpinLock);PAISQSL AcquireInStackQueued = NULL; ... 

此程式碼範例(驅動程式的初始化代碼)確定驅動程式是否在 Windows 7 或更高版本的作業系統上運行。如果是,代碼會檢索指向 KeAcquireInStackQueuedSpinLock 的指標。

  
... //// Are we running on Windows 7 or later?// if (RtlIsNtDdiVersionAvailable(NTDDI_WIN7) ) {  //  // Yes... Windows 7 or later it is!  //     RtlInitUnicodeString(&funcName,                  L"KeAcquireInStackQueuedSpinLock");  //  // Get a pointer to Windows implementation  // of KeAcquireInStackQueuedSpinLock into our  // variable "AcquireInStackQueued"     AcquireInStackQueued = (PAISQSL)                  MmGetSystemRoutineAddress(&funcName); } ...// Acquire a spin lock.  if( NULL != AcquireInStackQueued) {  (AcquireInStackQueued)(&SpinLock, &lockHandle);} else {    KeAcquireSpinLock(&SpinLock);} 

在該樣本中,驅動程式調用 RtlIsNtDdiVersionAvailable 以確定驅動程式是否在 Windows 7 或更高版本上運行。如果版本為 Windows 7 或更高版本,驅動程式將會調用 MmGetSystemRoutineAddress 來擷取指向 KeAcquireInStackQueuedSpinLock 函數的指標並將此指標儲存在名為 AcquireInStackQueued 的變數(該變數聲明為 PAISQSL 類型)中。

然後,當驅動程式必須擷取旋轉鎖時,它會檢查是否收到指向 KeAcquireInStackQueuedSpinLock 函數的指標。如果驅動程式已收到此指標,則驅動程式會使用該指標調用 KeAcquireInStackQueuedSpinLock。如果指向 KeAcquireInStackQueuedSpinLock 的指標為 Null,則驅動程式會使用 KeAcquireSpinLock 來擷取旋轉鎖。

 

為不同版本的 Windows 編寫驅動程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.