///////////////////////////////////// #include "stdafx.h" #include "RegDemo.h" #include "RegDemoDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //注冊(cè)表操作時(shí)用的變量; HKEY hKey; char content[256]; //所查詢(xún)注冊(cè)表鍵值的內(nèi)容 DWORD dwType=REG_SZ; //定義讀取數(shù)據(jù)類(lèi)型 DWORD dwLength=256; HKEY RootKey; //注冊(cè)表主鍵名稱(chēng) TCHAR *SubKey; //欲打開(kāi)注冊(cè)表項(xiàng)的地址 TCHAR *KeyName; //欲設(shè)置項(xiàng)的名字 TCHAR *ValueName; //欲設(shè)置值的名稱(chēng) LPBYTE SetContent_S; //字符串類(lèi)型 int SetContent_D[256]; //DWORD類(lèi)型 BYTE SetContent_B[256]; //二進(jìn)制類(lèi)型 ///////////設(shè)置注冊(cè)表操作函數(shù); int ShowContent ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName); int SetValue_S ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName, LPBYTE ReSetContent_S); int SetValue_D ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]); int SetValue_B ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName, BYTE ReSetContent_B[256]); int DeleteKey ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName); int DeleteValue ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName); /////////////////////////////////////////////// ShowContent ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName) {//查看鍵值的函數(shù); int i=0; //操作結(jié)果:0==succeed if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS) { if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(unsigned char *) content,&dwLength)!=ERROR_SUCCESS) { AfxMessageBox("錯(cuò)誤:無(wú)法查詢(xún)有關(guān)的注冊(cè)表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("錯(cuò)誤:無(wú)法打開(kāi)有關(guān)的hKEY"); i=1; } return i; } SetValue_S ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S) { //設(shè)置字符串值函數(shù) int i=0; //操作結(jié)果: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("錯(cuò)誤:無(wú)法設(shè)置有關(guān)的注冊(cè)表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("錯(cuò)誤:無(wú)法查詢(xún)有關(guān)的注冊(cè)表信息"); i=1; } return i; }
SetValue_D ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName, int ReSetContent_D[256]) { //設(shè)置DWORD值函數(shù) int i=0; //操作結(jié)果:0==succeed if(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("錯(cuò)誤:無(wú)法設(shè)置有關(guān)的注冊(cè)表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("錯(cuò)誤:無(wú)法查詢(xún)有關(guān)的注冊(cè)表信息"); i=1; } return i; } SetValue_B (HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]) { //設(shè)置二進(jìn)制值函數(shù) int i=0; //操作結(jié)果:0==succeed if(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("錯(cuò)誤:無(wú)法設(shè)置有關(guān)的注冊(cè)表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("錯(cuò)誤:無(wú)法查詢(xún)有關(guān)的注冊(cè)表信息"); i=1; } return i; }
DeleteKey ( HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName) { //刪除子項(xiàng)函數(shù) int i=0; //操作結(jié)果:0==succeed if((RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey))==ERROR_SUCCESS) { if((RegDeleteKey(hKey,ReKeyName))!=ERROR_SUCCESS) { //AfxMessageBox("清除指定項(xiàng)失�。�"); i=1; } RegCloseKey(hKey); } else { //AfxMessageBox("錯(cuò)誤:無(wú)法打開(kāi)有關(guān)的hKEY"); i=1; } return i; }
DeleteValue (HKEY ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName) { //刪除鍵值函數(shù) int i=0; //操作結(jié)果:0==succeed if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS) { if(RegDeleteValue(hKey,ReValueName)!=ERROR_SUCCESS) { //AfxMessageBox("清除指定值失�。�"); i=1; } RegCloseKey(hKey); } else { //AfxMessageBox("錯(cuò)誤:無(wú)法打開(kāi)有關(guān)的hKEY"); i=1; } return i; } /////////////////////////////////// void CRegDemoDlg::OnSetValue_S() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例1"; //欲設(shè)置值的名稱(chēng) SetContent_S=LPBYTE("Visual C++編程實(shí)例"); //值的內(nèi)容 if((SetValue_S(RootKey,SubKey,ValueName,SetContent_S))!=0) AfxMessageBox("操作失��!"); } void CRegDemoDlg::OnSetContent_B() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例2"; //欲設(shè)置值的名稱(chēng) SetContent_B[0]=1; //值的內(nèi)容 if((SetValue_B(RootKey,SubKey,ValueName,SetContent_B))!=0) AfxMessageBox("操作失��!"); }
void CRegDemoDlg::OnSetContent_D() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例3"; //欲設(shè)置值的名稱(chēng) SetContent_D[0]=4294967295; //值的內(nèi)容 if((SetValue_D(RootKey,SubKey,ValueName,SetContent_D))!=0) AfxMessageBox("操作失��!"); }
void CRegDemoDlg::OnDeleteValue_1() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例1"; //欲設(shè)置值的名稱(chēng) if((DeleteValue (RootKey,SubKey,ValueName))!=0) AfxMessageBox("操作失�。�"); }
void CRegDemoDlg::OnDeleteValue_2() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例2"; //欲設(shè)置值的名稱(chēng) if((DeleteValue (RootKey,SubKey,ValueName))!=0) AfxMessageBox("操作失��!"); }
void CRegDemoDlg::OnDeleteValue_3() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例3"; //欲設(shè)置值的名稱(chēng) if((DeleteValue (RootKey,SubKey,ValueName))!=0) AfxMessageBox("操作失�。�"); } void CRegDemoDlg::OnDeleteKey() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"; //欲打開(kāi)注冊(cè)表值的地址 KeyName="Doc Find Spec MRU"; //欲設(shè)置項(xiàng)的名稱(chēng) if((DeleteKey (RootKey,SubKey,KeyName))!=0) AfxMessageBox("操作失��!"); } void CRegDemoDlg::OnShowContent() { // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注冊(cè)表主鍵名稱(chēng) SubKey="Software\\Microsoft"; //欲打開(kāi)注冊(cè)表值的地址 ValueName="例1"; //欲設(shè)置值的名稱(chēng) if ((ShowContent(RootKey,SubKey,ValueName))==0) MessageBox(content,"本操作是利用ShowContent()函數(shù)完成的。"); } |