今天剛學了對系統資料庫的讀寫操作。在這裡總結一下。
主要用到的函數有:
LONG WINAPI RegCreateKey( //Creates the specified registry key. If the key already exists in the registry, the function opens it.
__in HKEY hKey, //建立一個指定的系統資料庫項,如果這個表項已經存在,則打開它
__in_opt LPCTSTR lpSubKey,
__out PHKEY phkResult
);
LONG WINAPI RegSetValue( //Sets the data for the default or unnamed value of a specified registry key. The data must be a text string.
__in HKEY hKey, //設定一個指定的預設的或者是未命名的系統資料庫項的值。這個值必須是文本串,也就是說隻能寫入文本類型的值
__in_opt LPCTSTR lpSubKey,
__in DWORD dwType,
__in_opt LPCTSTR lpData,
__in DWORD cbData
);
LONG WINAPI RegQueryValue( //Retrieves the data associated with the default or unnamed value of a specified registry
__in HKEY hKey, // The data must be a null-terminated string
__in_opt LPCTSTR lpSubKey, //獲得一個指定的預設的或者是未命名的系統資料庫項的值,這個值必須是文本串
__out_opt LPTSTR lpValue, //與RegSetValue相對應
__inout_opt PLONG lpcbValue
);
實作:
1.首先調用RegCreateKey()函數,建立指定的系統資料庫項,代碼如下: HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
參數說明: 第一個參數hKey的取值可以是下面的這些,這些值在系統資料庫中都可看到,如下:
- HKEY_CLASSES_ROOT
- HKEY_CURRENT_CONFIG
- HKEY_CURRENT_USER
- HKEY_LOCAL_MACHINE
- HKEY_USERS
第二個參數可以了解為在上面那些檔案夾的路徑,代碼中"Software\\Lin\\admin",如果沒有找到相應的檔案夾,則建立。
第三個參數是個接收句柄,得到的句柄在下面要用到。
2.然後調用RegSetValue()函數,寫入想寫入的字元串值。(完成了資料的寫入)代碼如下: RegSetValue(hKey, "hello", REG_SZ, "hi", strlen("hi"));
寫入後:
參數說明:
第一個參數不用說了,就是上面得到的句柄。
第二個參數是上面得到的句柄的一個子項,可以了解為基于上面路徑的子檔案夾。比如上面的"Software\\Lin\\admin",如果想在admin下再繼續建立子項(檔案夾)可以繼續寫上子項(檔案夾)的名稱。如果是直接在admin中寫入資料,則用
NULL來表示預設路徑。
第三個參數就是要寫入的字元串值了。
第四個是寫入的長度。
注:在不需要用到hKey時要調用RegCloseKey(hKey);來關閉它。
3.在寫入之後,就要測試讀取了。調用RegQueryValue()函數即可。方法有兩種。代碼如下:
LONG lValue;<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//方法一</span>
char *pBuf;
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
RegQueryValue(hKey, "hello", NULL, &lValue);
pBuf = new char[lValue];
RegQueryValue(hKey, "hello", pBuf, &lValue);
MessageBox(pBuf);
LONG lValue;<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//方法二</span>
char *pBuf;
CString path = "Software\\Lin\\admin\\hello";
RegQueryValue(HKEY_LOCAL_MACHINE, path, NULL, &lValue);
pBuf = new char[lValue];
RegQueryValue(HKEY_LOCAL_MACHINE, path, pBuf, &lValue);
MessageBox(pBuf);
說明:兩種方法其實基本上相同,第一種方法調用了RegOpenKey()函數,打開一個表項,用hKey接收。第二種方法直接調用RegQueryValue()讀取。寫第一種方法主要是為了下面拓展函數讀取時的用法的一緻。
RegQueryValue()函數參數說明:
第一,第二個參數跟上面所說的一樣。
第三個參數是一個接收值。值得說明的是:當這個值被設定為NULL,而第四個參數不為NULL時,第四個參數将得到所要在系統資料庫中獲得的值的長度(包括結束符)
第四個參數也是一個接收值。即獲得所讀取資料的長度。
這樣,就完成了資料表的寫入與讀取操作了。
另外是拓展函數:
LONG WINAPI RegSetValueEx( //Sets the data and type of a specified value under a registry key.
__in HKEY hKey, //這個函數可以寫入多種類型的值
__in_opt LPCTSTR lpValueName,
__reserved DWORD Reserved,
__in DWORD dwType,
__in_opt const BYTE* lpData,
__in DWORD cbData
);
LONG WINAPI RegOpenKey( //Opens the specified registry key.
__in HKEY hKey,
__in_opt LPCTSTR lpSubKey,
__out PHKEY phkResult
);
LONG WINAPI RegQueryValueEx( //Retrieves the type and data for the specified value name associated with an open registry key.
__in HKEY hKey, //在獲得資料之前要想用RegOpenKey()函數來打開指定的系統資料庫項
__in_opt LPCTSTR lpValueName,
__reserved LPDWORD lpReserved,
__out_opt LPDWORD lpType,
__out_opt LPBYTE lpData,
__inout_opt LPDWORD lpcbData
);
拓展函數比上面的函數強大多了。不僅僅可以寫入文本值,也可以寫入其他類型的值。這裡舉個寫入32位整型的例子。
實作:
1.首先也是先調用RegCreateKey()函數,建立指定的系統資料庫項,代碼如下: HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
2.接着調用RegSetValueEx()函數,代碼如下:
DWORD dwAge = 30;
RegSetValueEx(hKey, "age", 0, REG_DWORD, (CONST BYTE*)&dwAge, sizeof(dwAge));
RegSetValueEx(hKey, "hello", 0, REG_SZ, (CONST BYTE*)"hello", strlen("helle"));
RegSetValueEx()參數說明:
第一個參數,和RegSetValue()函數相同。
第二個參數要注意的是,它不完全相同與RegSetValue()函數的第二個參數。上面代碼中的age,hello是位于admin下被寫入資料的名稱,而不是admin的子項(檔案夾)。
第三個參數是一個保留值,要設定為0。
第四個參數為要寫入的資料類型,其取值如下圖。
第五個參數為要寫入的資料。由于是CONST BYTE*類型,是以要強制轉換。
第六個參數為寫入資料的大小。
3.讀取操作。如下代碼:
HKEY hKey;
DWORD dwType;
DWORD dwAge;
DWORD dwValue;
RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
RegQueryValueEx(hKey, "age", NULL, &dwType, (LPBYTE)&dwAge, &dwValue);
CString str;
str.Format("%d", dwAge);
MessageBox(str);
搞定。。。。
最後再總結一下資料表項和資料的删除。
這裡用到兩個函數:
LONG WINAPI RegDeleteKey( //Deletes a subkey and its values. Note that key names are not case sensitive.
__in HKEY hKey,
__in LPCTSTR lpSubKey
);
LONG WINAPI RegDeleteValue( //Removes a named value from the specified registry key. Note that value names are not case sensitive.
__in HKEY hKey,
__in_opt LPCTSTR lpValueName
);
說明:從函數的名稱可以看出,一個是删除表項的,一個是删除資料名稱的。比如下面代碼:
HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
RegDeleteKey(hKey, "hello"); //删除的是上面第一幅圖的hello項
RegDeleteValue(hKey, "hello"); //删除的是admin下的hello
這樣,就把這個基本上總結完了哈。。。