VC 讀寫註冊表

來源:互聯網
上載者:User
近來由於需要在自己寫的程式中對註冊表進行操作。總結些經驗,並做個一個DEMO供日後使用,現在把它拿出來和大家分享…

為了使用方便,把一些操作寫成了函數,以便方便調用,具體代碼如下所示:

 

一、定義

HKEY hKey;char content[256];//所查詢註冊表索引值的內容DWORD dwType=REG_SZ;//定義讀取資料類型DWORD dwLength=256;struct HKEY__*RootKey;//註冊表主鍵名稱TCHAR *SubKey;//欲開啟登錄機碼的地址TCHAR *KeyName;//欲設定項的名字TCHAR *ValueName;//欲設定值的名稱LPBYTE SetContent_S;//字串類型int SetContent_D[256];//DWORD類型BYTE SetContent_B[256];//二進位類型
int ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);int SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S);int SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]);int SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]);int DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName);int DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);

 

二、查看函數

ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName){int i=0;//操作結果:0==succeedif(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS){if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(unsigned char *)content,&dwLength)!=ERROR_SUCCESS){AfxMessageBox("錯誤:無法查詢有關的註冊表資訊");i=1;}RegCloseKey(hKey);}else{AfxMessageBox("錯誤:無法開啟有關的hKEY");i=1;}return i;}

 

三、設定字串值函數

SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S){int i=0;//操作結果:0==succeed//int StrLength;//StrLength=CString(SetContent_S).GetLength();if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS){if(RegSetValueEx(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,CString(SetContent_S).GetLength())!=ERROR_SUCCESS){AfxMessageBox("錯誤:無法設定有關的註冊表資訊");i=1;}RegCloseKey(hKey);}else{AfxMessageBox("錯誤:無法查詢有關的註冊表資訊");i=1;}return i;}

 

四、設定DWORD值函數

SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]){int i=0;//操作結果:0==succeedif(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS){if(RegSetValueEx(hKey,ReValueName,NULL,REG_DWORD,(const unsigned char *)ReSetContent_D,4)!=ERROR_SUCCESS){AfxMessageBox("錯誤:無法設定有關的註冊表資訊");i=1;}RegCloseKey(hKey);}else{AfxMessageBox("錯誤:無法查詢有關的註冊表資訊");i=1;}return i;}

 

五、設定二進位值函數

SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]){int i=0;//操作結果:0==succeedif(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS){if(RegSetValueEx(hKey,ReValueName,NULL,REG_BINARY,(const unsigned char *)ReSetContent_B,4)!=ERROR_SUCCESS){AfxMessageBox("錯誤:無法設定有關的註冊表資訊");i=1;}RegCloseKey(hKey);}else{AfxMessageBox("錯誤:無法查詢有關的註冊表資訊");i=1;}return i;}

 

六、刪除子項函數

DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName){int i=0;//操作結果:0==succeedif((RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey))==ERROR_SUCCESS){if((RegDeleteKey(hKey,ReKeyName))!=ERROR_SUCCESS){//AfxMessageBox("清除指定項失敗!");i=1;}RegCloseKey(hKey);}else{//AfxMessageBox("錯誤:無法開啟有關的hKEY");i=1;}return i;}

七、刪除索引值函數

DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName){int i=0;//操作結果:0==succeedif(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS){if(RegDeleteValue(hKey,ReValueName)!=ERROR_SUCCESS){//AfxMessageBox("清除指定值失敗!");i=1;}RegCloseKey(hKey);}else{//AfxMessageBox("錯誤:無法開啟有關的hKEY");i=1;}return i;}

八、調用方法

void CRegDemoDlg::OnSetValue_S() //例1所使用的代碼:設定字串值{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例1";               //欲設定值的名稱SetContent_S=LPBYTE("成功");   //值的內容if((SetValue_S(RootKey,SubKey,ValueName,SetContent_S))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnSetContent_B() //例2所使用的代碼:設定二進位值{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例2";               //欲設定值的名稱SetContent_B[0]=1;             //值的內容//SetContent_B[1]=0x1B;//SetContent_B[2]=0x2C;//SetContent_B[3]=0x3D;//SetContent_B[4]=0x4E;if((SetValue_B(RootKey,SubKey,ValueName,SetContent_B))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnSetContent_D() //例3所使用的代碼:設定DWORD值{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例3";               //欲設定值的名稱SetContent_D[0]=4294967295;             //值的內容if((SetValue_D(RootKey,SubKey,ValueName,SetContent_D))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnDeleteValue_1() //例4所使用的代碼{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例1";               //欲設定值的名稱if((DeleteValue (RootKey,SubKey,ValueName))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnDeleteValue_2() //例4所使用的代碼{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例2";               //欲設定值的名稱if((DeleteValue (RootKey,SubKey,ValueName))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnDeleteValue_3() //例4所使用的代碼{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例3";               //欲設定值的名稱if((DeleteValue (RootKey,SubKey,ValueName))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnDeleteKey() //例5所使用的代碼{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;                                        //註冊表主鍵名稱SubKey="Software//Microsoft//Windows//CurrentVersion//Explorer";  //欲開啟註冊表值的地址KeyName="Doc Find Spec MRU";                                      //欲設定項的名稱if((DeleteKey (RootKey,SubKey,KeyName))!=0)AfxMessageBox("操作失敗!");}void CRegDemoDlg::OnShowContent() //例1中的[查看]{// TODO: Add your control notification handler code hereRootKey=HKEY_CURRENT_USER;     //註冊表主鍵名稱SubKey="Software//Microsoft";  //欲開啟註冊表值的地址ValueName="例1";               //欲設定值的名稱if ((ShowContent(RootKey,SubKey,ValueName))==0)MessageBox(content,"本操作是利用ShowContent()函數完成的。");

 

 

參考推薦:

對註冊表進行簡單的操作

如何在VC++中使用註冊表

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.