MFC操作注冊表


打開注冊表鍵

 

1 LONG RegOpenKeyEx(
2   HKEY hKey,         // handle to open key主鍵
3   LPCTSTR lpSubKey,  // subkey name子鍵
4   DWORD ulOptions,   // reserved。必須是0
5   REGSAM samDesired, // security access mask讀寫標識
6   PHKEY phkResult    // handle to open key返回的HKEY類型的指針。以后,讀寫,關閉用這個指針
7 );

 

 

如:

1 // 打開HKEY_LOCAL_MACHINE主鍵下的SoftWare\\Knight Studio\\Knight子鍵
2  HKEY hKEY;
3  HKEY  hKeyRoot = HKEY_LOCAL_MACHINE;
4  long ret0=(::RegOpenKeyEx(hKeyRoot,"SoftWare\\Knight Studio\\Knight",0,KEY_READ,&hKEY));
5  if(ret0!=ERROR_SUCCESS)//如果無法打開hKEY,則中止程序的執行
6  {
7   AfxMessageBox("錯誤:無法打開有關的hKEY");
8   return;
9  }

 

讀取注冊表

1 LONG RegQueryValueEx(
2   HKEY hKey,            // handle to key打開注冊表指針
3   LPCTSTR lpValueName,  // value name要讀取的鍵名稱
4   LPDWORD lpReserved,   // reserved  must be NULL. 必須是NULL
5   LPDWORD lpType,       // type buffer,鍵類型。我最常用REG_SZ,REG_DWORD
6   LPBYTE lpData,        // data buffer。保存查詢結果的緩沖區
7   LPDWORD lpcbData      // size of data buffer。緩沖區大小
8 );

 

如:

 1 // hKEY是上面打開時得到的指針。
 2  LPBYTE getValue = new BYTE[80];//得到的鍵值
 3  DWORD keyType = REG_SZ;//定義數據類型
 4  DWORD DataLen = 80;//定義數據長度
 5  CString strUser = _T("UserName");//要查詢的鍵名稱
 6  long ret1=::RegQueryValueEx(hKEY,strUser,NULL,&keyType,getValue,&DataLen);
 7  if(ret1!=ERROR_SUCCESS)
 8  {
 9   AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
10   return;
11  }

 

寫注冊表

1 LONG RegSetValueEx(
2   HKEY hKey,           // handle to key。打開注冊表的指針
3   LPCTSTR lpValueName, // value name 要寫入的鍵
4   DWORD Reserved,      // reserved  必須是0
5   DWORD dwType,        // value type 寫入值類型
6   CONST BYTE *lpData,  // value data 要寫入的數據
7   DWORD cbData         // size of value data 。數據SIZE
8 );

 

如:

 1 // 寫注冊表。修改UserName為Knight
 2 // 寫入CString類型的數據
 3 CString strUser = _T("UserName");//要寫入的鍵名稱
 4 LPCTSTR strUserValue = "Knight";
 5 long ret = ::RegSetValueEx(hKEY, strUser, 0, REG_SZ, (const BYTE *) strUserValue, strlen(strUserValue)+1);
 6 if(ret1!=ERROR_SUCCESS)
 7 {
 8  AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
 9  return;
10 }

 

創建一個新鍵

 1 LONG RegCreateKeyEx(
 2   HKEY hKey,                                  // handle to open key。打開的注冊表指針
 3   LPCTSTR lpSubKey,                           // subkey name。子鍵名稱
 4   DWORD Reserved,                             // reserved。必須為0
 5   LPTSTR lpClass,                             // class string。已經存在時用,一般為NULL
 6   DWORD dwOptions,                            // special options
 7                //默認值REG_OPTION_VOLATILE,保存在注冊表,下次開機仍然存在
 8                //REG_OPTION_VOLATILE,保存在內存
 9                //REG_OPTION_BACKUP_RESTORE
10   REGSAM samDesired,                          // desired security access。操作權限。一般KEY_ALL_ACCESS,除非有特殊需要,請查閱MSDN
11   LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance。繼承性。一般為NULL
12   PHKEY phkResult,                            // key handle 。返回該鍵值鎮。
13   LPDWORD lpdwDisposition                     // disposition value buffer
14              //REG_CREATED_NEW_KEY The key did not exist and was created. 
15            //REG_OPENED_EXISTING_KEY The key existed and was simply opened without being changed.
16 
17 );

 

刪除一個鍵

1 LONG RegDeleteKey(
2   HKEY hKey,         // handle to open key
3   LPCTSTR lpSubKey   // subkey name
4 );

 

刪除一個鍵值

1 LONG RegDeleteValue(
2   HKEY hKey,            // handle to key
3   LPCTSTR lpValueName   // value name。值名稱,不是打開的那個指針,是查詢到的指針,如果為空RegSetValueEx創建的值將被刪除
4 );

 

刷新注冊表

1 LONG RegFlushKey(
2   HKEY hKey   // handle to key to write。寫入所有的值,在給定的指針
3 );

 

導入一個注冊表文件到指定的鍵下

1 LONG RegLoadKey(
2   HKEY hKey,        // handle to open key
3   LPCTSTR lpSubKey, // subkey name
4   LPCTSTR lpFile    // registry file name
5 );

 

關閉打開的注冊表

1 LONG RegCloseKey(
2   HKEY hKey   // handle to key to close
3 );

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM