VC讀寫註冊表

來源:互聯網
上載者:User
讀寫註冊表
2003-11-3加入  來自未知  作者佚名  7條評論  點擊12953次
       Win 95及NT的註冊資料庫(Registry)是系統中非常重要的組成部分。在Win32 API中有一組Reg函數來處理這些問題。其一般的讀寫過程如下:
 
    1、使用RegOpenKeyEx或RegCreateKeyEx函數開啟或建立一個鍵; 
    2、如果上一步成功,使用RegQueryValueEx讀取子鍵的值,使用RegSetValueEx設定子索引值,使用RegEnumKey獲得所有子鍵,使用RegDeleteKey刪除一個鍵; 
    3、完成操作後使用RegCloseKey關閉鍵。 
    下面這段程式開啟HKEY_CURRENT_USER\Software\Zeal SoftStudio\AskPro FTP\LastTime鍵,然後讀取WOL子鍵的值。 

    HKEY hkey; 
    char sz[256]; 
    DWORD dwtype, sl = 256; 
     
    RegOpenKeyEx(HKEY_CURRENT_USER, 
    "Software\\Zeal SoftStudio\\AskPro FTP\\LastTime", 
    NULL, KEY_ALL_ACCESS, &hkey); 
    RegQueryValueEx(hkey, "WOL", NULL, &dwtype, (LPBYTE)sz, &sl); 
    RegCloseKey(hkey); 
    MFC程式可以使用CRegKey類讀寫註冊表。VB中調用API的辦法可以參考QA000226 "如何訪問Windows系統註冊表"。

    開啟註冊鍵
    LONG RegOpenKeyEx( HKEY hKey,  // handle to open key 

    LPCTSTR lpSubKey,              // address of name of subkey to open 
    DWORD ulOptions,               // reserved =0
    REGSAM samDesired,             // security access mask 
    PHKEY phkResult                // address of handle to open key 
    );

    例:
    HKEY hd;
    hd=HKEY_LOCAL_MACHINE;
    char* Regkeyname="SoftWare\\Xy123\\Poker\\";
    LONG a=RegOpenKeyEx(hd,Regkeyname,0,KEY_READ,&hd);   //成功返回ERROR_SUCCESS,否則返回錯誤碼
 

    關閉註冊鍵
    LONG RegCloseKey( HKEY hKey // handle to key to close );
例:
     RegCloseKey(HKEY_LOCAL_MACHINE);
OR:  RegCloseKey(hd); 
建立註冊鍵
LONG RegCreateKeyEx( HKEY hKey, // handle to an open key 
      LPCTSTR lpSubKey, // address of subkey name 
      DWORD Reserved, // reserved =0
      LPTSTR lpClass, // address of class string 
      DWORD dwOptions, // special options flag 
      REGSAM samDesired, // desired security access 

      LPSECURITY_ATTRIBUTES lpSecurityAttributes, // address of key security         structure 
      PHKEY phkResult, // address of buffer for opened handle 
      LPDWORD lpdwDisposition // address of disposition value buffer );
例:
   char *sclass="";  //類名指定為空白
   DWORD nbf=0;    //接受傳回值,指明是建立新鍵還是開啟已有的鍵.(經實驗總是返回REG_OPENED_EXISTING_KEY.
   LONG II=RegCreateKeyEx(hd,Regkeyname,0,sclass,REG_OPTION_NON_VOLATILE,
                KEY_READ|KEY_WRITE,NULL,&hd,&nbf);

//REG_OPTION_NON_VOLATILE 指明鍵持續保留.安全結構指明NULL,自動獲得一預設值
//成功返回ERROR_SUCCESS,否則返回錯誤碼 
枚舉索引值
LONG RegEnumValue( HKEY hKey, // handle to key to query 
      DWORD dwIndex, // index of value to query 
      LPTSTR lpValueName, // address of buffer for value string 
      LPDWORD lpcbValueName, // address for size of value buffer 
      LPDWORD lpReserved, // reserved =NULL
      LPDWORD lpType, // address of buffer for type code 

      LPBYTE lpData, // address of buffer for value data 
      LPDWORD lpcbData // address for size of data buffer);
例:
   DWORD dinx=0;
   char valuename[70];  //分配數值名稱緩衝區
   strcpy(valuename,"DeskPattern");  //隨便指定哪個索引值名
   DWORD nsize=69;  //數值名稱緩衝區大小
   DWORD k=REG_SZ;  //指明資料類型
   unsigned char vari[70]; //分配數值緩衝區
   DWORD ncbvari=69; //數值緩衝區大小
   dinx=0; //從0開始

   while((II=RegEnumValue(hd,dinx,valuename,&nsize,NULL,&k,vari,&ncbvari)) 
          != ERROR_NO_MORE_ITEMS)
   {
       dinx++;//索引 +1,準備取下一個值
       nsize=69; //恢複原來大小
       ncbvari=69;
   }
成功後傳回值0,各變數返回後設定如下:
valuename=數值名稱,以0結尾;如 : DeskColor
nsize=數值名稱長度, 9
k=REG_SZ  DeskColor 的類型為 REG_SZ
vari=索引值,32768 DeskColor="32768",
ncbvari=索引值長度 REG_SZ包括結尾0,=6, 
讀取索引值
LONG RegQueryValueEx( HKEY hKey, // handle to key to query 

       LPTSTR lpValueName, // address of name of value to query 
       LPDWORD lpReserved, // reserved 
       LPDWORD lpType, // address of buffer for value type 
       LPBYTE lpData, // address of data buffer 
       LPDWORD lpcbData // address of data buffer size );
例:
   RegQueryValueEx(hd,valuename,NULL,&k,vari,&ncbvari);
變數定義及成功後各變數設定值同RegEnumValueEx 
寫索引值
LONG RegSetValueEx( HKEY hKey, // handle to key to set value for 
       LPCTSTR lpValueName, // name of the value to set 

       DWORD Reserved, // reserved 
       DWORD dwType, // flag for value type 
       CONST BYTE *lpData, // address of value data 
       DWORD cbData // size of value data );
例:
   strcpy(valuename,"Hello");
   unsigned char vari[10];
   DWORD k=REG_SZ;
   strcpy((char*)vari,"1234567")
   RegSetValueEx(hd,valuename,0,k,vari,7);
成功後在Poker下增加一個索引值 Hello : REG_SZ : 1234567
寫整型變數:
int hi=8;
RegSetValueEx(pj,valuename,0,REG_BINARY,(unsigned char*)&hi,sizeof(int));

成功後在Poker下增加一個索引值 Hello2 : REG_BINARY :08 00 00 00

void AddEventSource()
{
    HKEY hk; 
    DWORD dwData; 
    UCHAR szBuf[80]; 

    // Add your source name as a subkey under the Application 
    // key in the EventLog registry key. 

    if (RegCreateKey(HKEY_LOCAL_MACHINE, 
            "SYSTEM\\CurrentControlSet\\Services\ 
            \\EventLog\\Application\\SamplApp", &hk)) 
        ErrorExit("Could not create the registry key."); 

    // Set the name of the message file. 

    strcpy(szBuf, "%SystemRoot%\\System\\SamplApp.dll"); 

    // Add the name to the EventMessageFile subkey. 

    if (RegSetValueEx(hk,             // subkey handle 
            "EventMessageFile",       // value name 
            0,                        // must be zero 
            REG_EXPAND_SZ,            // value type 
            (LPBYTE) szBuf,           // pointer to value data 
            strlen(szBuf) + 1))       // length of value data 

        ErrorExit("Could not set the event message file."); 

    // Set the supported event types in the TypesSupported subkey. 

    dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | 
        EVENTLOG_INFORMATION_TYPE; 

    if (RegSetValueEx(hk,      // subkey handle 
            "TypesSupported",  // value name 
            0,                 // must be zero 
            REG_DWORD,         // value type 
            (LPBYTE) &dwData,  // pointer to value data 

            sizeof(DWORD)))    // length of value data 
        ErrorExit("Could not set the supported types."); 

    RegCloseKey(hk); 

以下代碼把註冊表自啟動shell的索引值改寫為C:\DK1\ATM\HARP\ExAtmShell.exe:

         HKEY hkey;
LONG res; 
DWORD datatype=REG_SZ; 
unsigned char szvalue[_MAX_PATH];
strcpy((char*)szvalue,"C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");

res =::RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\", 0, 
KEY_WRITE|KEY_READ, &hkey); 

if(res!=ERROR_SUCCESS)
{
AfxMessageBox("aaa");
return;
}
res = ::RegSetValueEx(hkey, "Shell", 0, datatype, szvalue, strlen(LPCSTR(szvalue))); 

RegCloseKey(hkey);
if(res==ERROR_SUCCESS)
::AfxMessageBox("你已經成功地將註冊表自啟動shell的索引值設定為C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");
else
::AfxMessageBox("設定失敗:目標位置不存在這樣的鍵!");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.