1. 程式人生 > >Windows API一日一練 64 RegSetValueEx和RegDeleteValue函數

Windows API一日一練 64 RegSetValueEx和RegDeleteValue函數

lpcwstr 字符 app 名稱 註冊 pcs 一個 dword 基礎

上一次說到怎麽創建註冊表的鍵,但接著下來的問題就是怎麽樣保存數據到註冊表裏。註冊表使用樹形的方式管理數據,所以它的擴展和訪問都是比較靈活的。不過註冊表是系統重要信息庫,每當Windows系統加載時,都首先從硬盤裏讀取它出來,才知道每臺電腦所有硬件配置信息,然後再加載不同的驅動程序。因此,註冊表作為系統重要的文件,不要往裏面寫超過2K的數據大小,這樣可以提高系統的速度。下面就來介紹一下怎麽樣保存一個字符串的鍵值。它需要使用RegSetValueEx函數來設置鍵值和使用RegDeleteValue函數來刪除原來的鍵值。 函數RegSetValueExRegDeleteValue聲明如下: WINADVAPI
LONG APIENTRY RegSetValueExA ( __in HKEY hKey, __in_opt LPCSTR lpValueName, __reserved DWORD Reserved, __in DWORD dwType, __in_bcount_opt(cbData) CONST BYTE* lpData, __in DWORD cbData ); WINADVAPI LONG APIENTRY RegSetValueExW ( __in HKEY hKey, __in_opt LPCWSTR lpValueName,
__reserved DWORD Reserved, __in DWORD dwType, __in_bcount_opt(cbData) CONST BYTE* lpData, __in DWORD cbData ); #ifdef UNICODE #define RegSetValueEx RegSetValueExW #else #define RegSetValueEx RegSetValueExA #endif // !UNICODE WINADVAPI LONG APIENTRY RegDeleteValueA ( __in HKEY hKey,
__in_opt LPCSTR lpValueName ); WINADVAPI LONG APIENTRY RegDeleteValueW ( __in HKEY hKey, __in_opt LPCWSTR lpValueName ); #ifdef UNICODE #define RegDeleteValue RegDeleteValueW #else #define RegDeleteValue RegDeleteValueA #endif // !UNICODE hKey是主鍵。 lpValueName是鍵名稱。 dwType是鍵值類型。 lpData是鍵的數據。 cbData是鍵值的大小。 調用函數的例子如下: #001 //打註冊表。HKEY_CURRENT_USER/"Software"/"Wincpp"/"testreg" #002 // /"Windows"//"winsize" = "800*600" #003 //蔡軍生 2007/11/04 QQ:9073204 深圳 #004 BOOL WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, #005 LPCTSTR lpszValue) #006 { #007 // #008 LONG lResult; #009 if (lpszEntry == NULL) //刪除整鍵。 #010 { #011 HKEY hAppKey = GetAppRegistryKey(); #012 if (hAppKey == NULL) #013 { #014 return FALSE; #015 } #016 #017 lResult = ::RegDeleteKey(hAppKey, lpszSection); #018 RegCloseKey(hAppKey); #019 } #020 else if (lpszValue == NULL) #021 { #022 //刪除鍵值。 #023 HKEY hAppKey = GetAppRegistryKey(); #024 if (hAppKey == NULL) #025 { #026 return FALSE; #027 } #028 #029 HKEY hSecKey = NULL; #030 DWORD dw; #031 RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE, #032 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, #033 &hSecKey, &dw); #034 RegCloseKey(hAppKey); #035 #036 if (hSecKey == NULL) #037 { #038 return FALSE; #039 } #040 #041 // #042 lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry); #043 RegCloseKey(hSecKey); #044 } #045 else #046 { #047 //設置鍵值。 #048 HKEY hAppKey = GetAppRegistryKey(); #049 if (hAppKey == NULL) #050 { #051 return FALSE; #052 } #053 #054 HKEY hSecKey = NULL; #055 DWORD dw; #056 //創建子鍵。 #057 RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE, #058 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, #059 &hSecKey, &dw); #060 RegCloseKey(hAppKey); #061 #062 if (hSecKey == NULL) #063 { #064 return FALSE; #065 } #066 #067 //設置子鍵中的項值。 #068 lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ, #069 (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)); #070 RegCloseKey(hSecKey); #071 } #072 return lResult == ERROR_SUCCESS; #073 #074 }

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://www.cnblogs.com/captainbed

Windows API一日一練 64 RegSetValueEx和RegDeleteValue函數