標籤:windows驅動 windows phone 8 註冊表 wdf
在做Windows Phone驅動開發時,時常需要和註冊表打交道,因此,作為手機驅動開發人員,有必要瞭解一下註冊表相關的知識。本人目前對註冊表也不怎麼瞭解,也只是大概的知道在Windows Phone下,驅動的inf檔案並不像案頭Windows那樣提供驅動自動安裝程式,在Windows Phone下,inf檔案最終會被解析成註冊表資訊並添加到註冊表中。我們驅動往往將一些硬體參數儲存在註冊表中,因此在軟體編程時,自然就會涉及到註冊表的讀寫更新操作。
本人目前在做觸控螢幕驅動時,就涉及到了註冊表的讀寫操作。為了方便以後查閱,這裡貼出註冊表的更新UpdateRegistry()和讀取ReadRegistry()函數代碼。
更新註冊表:
NTSTATUS __drv_requiresIRQL(PASSIVE_LEVEL) UpdateRegistry( __in WDFDEVICE FxDevice, __in PUNICODE_STRING SubkeyName, __in PUNICODE_STRING ValueName, __inout PUNICODE_STRING Value ){ NTSTATUS status; NTSTATUS RegFlushStatus; WDFKEY Key = NULL; WDFKEY Subkey = NULL; HANDLE KeyHandle; PAGED_CODE(); // // Find FileName from registry key // status = WdfDeviceOpenRegistryKey(FxDevice, PLUGPLAY_REGKEY_DEVICE, STANDARD_RIGHTS_ALL, WDF_NO_OBJECT_ATTRIBUTES, &Key); if (!NT_SUCCESS(status)) goto exit; status = WdfRegistryOpenKey(Key, SubkeyName, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &Subkey); if (!NT_SUCCESS(status)) goto exit; status = WdfRegistryAssignUnicodeString(Subkey, ValueName, Value); if (!NT_SUCCESS(status)) goto exit; //flush the key for immediate effect KeyHandle = WdfRegistryWdmGetHandle(Subkey); if( NULL != KeyHandle) { RegFlushStatus = ZwFlushKey(KeyHandle); }exit: if (Subkey != NULL) WdfRegistryClose(Subkey); if (Key != NULL) WdfRegistryClose(Key); return status;}
讀取註冊表:
NTSTATUS __drv_requiresIRQL(PASSIVE_LEVEL) ReadRegistry( __in WDFDEVICE FxDevice, __in PUNICODE_STRING SubkeyName, __in PUNICODE_STRING ValueName, __inout PUNICODE_STRING Value ){ NTSTATUS Status; WDFKEY Key = NULL; WDFKEY Subkey = NULL; PAGED_CODE(); // // Find FileName from registry key // Status = WdfDeviceOpenRegistryKey(FxDevice, PLUGPLAY_REGKEY_DEVICE, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &Key); if (!NT_SUCCESS(Status)) goto exit; Status = WdfRegistryOpenKey(Key, SubkeyName, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &Subkey); if (!NT_SUCCESS(Status)) goto exit; // // Get String // Status = WdfRegistryQueryUnicodeString(Subkey, ValueName, NULL, Value); if (!NT_SUCCESS(Status)) goto exit;exit: if (Subkey != NULL) WdfRegistryClose(Subkey); if (Key != NULL) WdfRegistryClose(Key); return Status;}
函數使用案例
inf檔案:
;; TouchDetectionDriver.inf;...[TouchDetectionDriver_Device.NT.HW]AddReg = FilterInst.NT.HW.AddReg[FilterInst.NT.HW.AddReg]HKR,%QtTouchRegPath%,"TouchControllerID",0x00000002,"FF"HKR,%QtTouchRegPath%,"ForceControllerProbe",0x00000002,"1"...[Strings]QtTouchRegPath = "Configuration File"...
該inf檔案最終會被解析成註冊表資訊,並在添加到系統註冊表中。可以看到,在子鍵Configuration File目錄下,存在兩個entry,它們分別為TouchControllerID和ForceControllerProbe,鍵值分別為"FF"和"1"。
驅動檔案:
NTSTATUS RegistryTest(WDFDEVICE FxDevice){ NTSTATUS status = STATUS_SUCCESS; UNICODE_STRING strControllerID; ULONG TouchControllerID = 0; WCHAR szIDBuffer[3] = {0}; UNICODE_STRING strForceProbe; ULONG ForceControllerProbe = 0; WCHAR szProbeBuffer[3] = {0}; DECLARE_CONST_UNICODE_STRING(SubkeyName, L"Configuration File"); DECLARE_CONST_UNICODE_STRING(KeyTouchControllerID, L"TouchControllerID"); DECLARE_CONST_UNICODE_STRING(KeyForceControllerProbeID, L"ForceControllerProbe"); //Read TouchControllerID RtlInitEmptyUnicodeString(&strControllerID, szIDBuffer, sizeof(szIDBuffer)); ReadRegistry(FxDevice,(PUNICODE_STRING)&SubkeyName,(PUNICODE_STRING)&KeyTouchControllerID,&strControllerID); RtlUnicodeStringToInteger(&strControllerID, 10, (PULONG)&TouchControllerID); TDDLog("TDD:ControllerIDFromRegistry:ControllerID Registry Value is %d\n", TouchControllerID); //Read ForceControllerProbe RtlInitEmptyUnicodeString(&strForceProbe, szProbeBuffer, sizeof(szProbeBuffer)); ReadRegistry(FxDevice,(PUNICODE_STRING)&SubkeyName,(PUNICODE_STRING)&KeyForceControllerProbeID, &strForceProbe); RtlUnicodeStringToInteger(&strForceProbe, 10, (PULONG)&ForceControllerProbe); TDDLog("TDD:ControllerIDFromRegistry:Force controller probe Register Value is %d\n", ForceControllerProbe); //Update TouchControllerID RtlIntegerToUnicodeString(1, 10, &strControllerID); UpdateRegistry(FxDevice, (PUNICODE_STRING)&SubkeyName, (PUNICODE_STRING)&KeyTouchControllerID, &strControllerID); //Update ForceControllerProbe RtlIntegerToUnicodeString(0, 10, &strForceProbe); UpdateRegistry(FxDevice, (PUNICODE_STRING)&SubkeyName, (PUNICODE_STRING)&KeyForceControllerProbeID, &strForceProbe); return status;}
Windows Phone 8.1 驅動開發——註冊表read/write