Sample Code
Windows Driver Samples/ Windows Filtering Platform Sample/ C++/ lib/ HelperFunctions_Registry.cpp/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | //////////////////////////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2012 Microsoft Corporation. All Rights Reserved. // // Module Name: // HelperFunctions_Registry.cpp // // Abstract: // This module contains functions which simplify registry operations. // // Private Functions: // // Public Functions: // HlprRegistryDeleteKey(), // HlprRegistryDeleteValue(), // HlprRegistryGetValue(), // HlprRegistrySetValue(), // // Author: // Dusty Harper (DHarper) // // Revision History: // // [ Month ][Day] [Year] - [Revision]-[ Comments ] // May 01, 2012 - 1.0 - Creation // //////////////////////////////////////////////////////////////////////////////////////////////////// #include "HelperFunctions_Include.h" /// . /** @helper_function="HlprRegistryDeleteValue" Purpose: Delete the Value from the specified Registry key. <br> <br> Notes: <br> <br> */ _Success_( return == NO_ERROR) UINT32 HlprRegistryDeleteValue(_In_ HKEY hKey, _In_opt_ PWSTR pSubKey, _In_ PWSTR pValueName) { UINT32 status = NO_ERROR; HKEY keyHandle = 0; if (pSubKey) { UINT32 dispositionValue = 0; status = RegCreateKeyEx(hKey, pSubKey, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &keyHandle, ( LPDWORD )&dispositionValue); if (status != NO_ERROR) { HlprLogError(L "HlprRegistryDeleteValue : RegCreateKeyEx() [status: %#x][subKey: %s]" , status, pSubKey); HLPR_BAIL; } } else keyHandle = hKey; status = RegDeleteValue(keyHandle, pValueName); if (status != NO_ERROR) { if (status != ERROR_FILE_NOT_FOUND) { HlprLogError(L "HlprRegistryDeleteValue : RegDeleteValue() [status: %#x][Value: %s%s%s]" , status, pSubKey ? pSubKey : L "" , pSubKey ? L "\\" : L "" , pValueName); HLPR_BAIL; } status = NO_ERROR; } HLPR_BAIL_LABEL: return status; } /** @helper_function="HlprRegistrySetValue" Purpose: Opens a handle to the key, creates the appropriate value, and closes the key. <br> <br> Notes: <br> <br> */ _Success_( return == NO_ERROR) UINT32 HlprRegistrySetValue(_In_ HKEY hKey, _In_ PWSTR pSubKey, _In_ UINT32 type, _In_opt_ PWSTR pValueName, _In_reads_bytes_(valueSize) BYTE * pValue, _In_ UINT32 valueSize) { UINT32 status = NO_ERROR; HKEY keyHandle = 0; UINT32 dispositionValue = 0; status = RegCreateKeyEx(hKey, pSubKey, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &keyHandle, ( LPDWORD )&dispositionValue); if (status != NO_ERROR) { HlprLogError(L "HlprRegistrySetValue : RegCreateKeyEx() [status: %#x][subKey: %s]" , status, pSubKey); HLPR_BAIL; } status = RegSetValueEx(keyHandle, pValueName, 0, type, pValue, valueSize); if (status != NO_ERROR) { HlprLogError(L "HlprRegistrySetValue : RegSetValueEx() [status: %#x][valueName: %s]" , status, pValueName ? pValueName : L "(Default)" ); HLPR_BAIL; } HLPR_BAIL_LABEL: RegCloseKey(keyHandle); return status; }; /** @helper_function="HlprRegistryGetValue" Purpose: Retrieves a value from the registry. <br> <br> Notes: <br> <br> */ _Success_( return == NO_ERROR) UINT32 HlprRegistryGetValue(_In_ HKEY hKey, _In_ PWSTR pSubKey, _In_opt_ PWSTR pValueName, _Inout_ REGISTRY_VALUE* pRegValue) { UINT32 status = NO_ERROR; status = RegGetValue(hKey, pSubKey, pValueName, RRF_RT_ANY, ( LPDWORD )&(pRegValue->type), ( PVOID )pRegValue->pBuffer, ( LPDWORD )&(pRegValue->size)); if (status != NO_ERROR) { HlprLogError(L "HlprRegistryGetValue : RegGetValue() [status: %#x][Value: %s\\%s]" , status, pSubKey, pValueName ? pValueName : L "(Default)" ); HLPR_BAIL_ON_FAILURE(status); } HLPR_BAIL_LABEL: return status; } /** @helper_function="HlprRegistryDeleteKey" Purpose: Delete the SubKey from the Registry. <br> <br> Notes: <br> <br> */ _Success_( return == NO_ERROR) UINT32 HlprRegistryDeleteKey(_In_ HKEY hKey, _In_ PWSTR pSubKey) { UINT32 status = NO_ERROR; status = RegDeleteKey(hKey, pSubKey); if (status != NO_ERROR) { if (status != ERROR_FILE_NOT_FOUND) { HlprLogError(L "HlprRegistryDeleteKey : RegDeleteKey() [status: %#x][subKey: %s]" , status, pSubKey); HLPR_BAIL; } status = NO_ERROR; } HLPR_BAIL_LABEL: return status; } |
Our Services
-
What our customers say about us?
Read our customer testimonials to find out why our clients keep returning for their projects.
View Testimonials