讀寫註冊表(轉)

 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("設定失敗:目標位置不存在這樣的鍵!");
發佈了23 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章