注冊表操作(VC_Win32)


數據類型

注冊表的數據類型主要有以下四種:
顯示類型(在編輯器中)   數據類型    說明                   
REG_SZ           字符串     文本字符串
REG_MULTI_SZ         多字符串    含有多個文本值的字符串
REG_BINARY          二進制數    二進制值,以十六進制顯示.
REG_DWORD           雙字      一個32位的二進制值,顯示為8位的十六進制值.

各主鍵的簡單介紹

  • HKEY_LOCAL_MACHINE  是一個顯示控制系統和軟件的處理鍵.HKLM鍵保存着計算機的系統信息.它包括網絡和硬件上所有的軟件設置.
  • HKEY_CLASSES_ROOT  是系統中控制所有數據文件的項.
  • HKEY_USERS  將缺省用戶和目前登陸用戶的信息輸入到注冊表編輯器
  • HKEY_CURRENT_USER  包含着在HKEY_USERS安全辨別里列出的同樣信息
  • HKEY_CURRENT_CONFIG  包括了系統中現有的所有配置文件的細節.HKEY_CURRENT_CONFIG允許軟件和設備驅動程序員很方便的更新注冊表,而不涉及到多個配置文件信息. HKEY_LOCAL_MACHINE中同樣的數據和任何注冊表的變化都會同時的變化.

 

相關函數

創建鍵 RegCreateKeyEx(次函數主要用於生成鍵(目錄))

函數原型

LONG RegCreateKeyEx(

  HKEYhKey,                                 // handle to open key

  LPCTSTRlpSubKey,                           // subkey name

  DWORDReserved,                             // reserved

  LPTSTRlpClass,                             // class string

  DWORD dwOptions,                            // special options

  REGSAMsamDesired,                          // desired security access

 LPSECURITY_ATTRIBUTES lpSecurityAttributes, //inheritance

  PHKEYphkResult,                            // key handle

  LPDWORD lpdwDisposition                     //disposition value buffer

);

參數說明

  • hKey:   要打開鍵的句柄或以下預定義句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpSubKey:   指向一個用於定義子鍵路徑的字符串
  • Reserved,dwOptions,samDesired:   置0
  • lpClass,lpSecurityAttributes:   置NULL
  • phkResult:   用於接收鍵句柄
  • lpdwDisposition:   接收的相關信息,取值如下
    • REG_CREATED_NEW_KEY   創建成功
    • REG_OPENED_EXISTING_KEY    鍵已存在

返回值:If the function succeeds, the return value is ERROR_SUCCESS.

打開鍵 RegOpenKeyEx

函數原型

LONG RegOpenKeyEx(

  HKEYhKey,         // handle to open key

  LPCTSTRlpSubKey,  //subkey name

  DWORDulOptions,   //reserved

  REGSAMsamDesired, // security access mask

  PHKEYphkResult    //handle to open key

);

參數說明

  • hKey:     要打開鍵的句柄或以下預定義句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpSubKey:   指向一個用於定義子鍵路徑的字符串
  • ulOptions:   保留位,置0
  • samDesired:   打開鍵后鍵的操作權限
  • phResult:   接收打開的鍵的句柄

返回值:If the function succeeds, the return value is ERROR_SUCCESS

刪除鍵 RegDeleteKey

函數原型

LONG RegDeleteKey(

  HKEYhKey,         // handle to open key

  LPCTSTRlpSubKey   //subkey name

);

參數說明

  • hKey:   要打開鍵的句柄或以下預定義句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpSubKey:   指向一個用於定義子鍵路徑的字符串

返回值:If the function succeeds, the return value is ERROR_SUCCESS

修改/添加鍵值 RegSetValueEx

函數原型

LONG RegSetValueEx(

  HKEYhKey,           // handle to key

  LPCTSTRlpValueName, // value name

  DWORDReserved,      //reserved

  DWORDdwType,        //value type

  CONST BYTE*lpData,  //value data

  DWORD cbData         // size ofvalue data

);

參數說明

  • hKey:   打開鍵的句柄或以下預定義句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpValueName:   鍵值的名稱
  • Reserved:   保留位,置0
  • dwType:   鍵值的類型
  • lpData:   鍵值
  • cbData:   鍵值數據長度

返回值:If the function succeeds, thereturn value is ERROR_SUCCESS

刪除鍵值 RegDeleteValue

函數原型

LONG RegDeleteValue(

  HKEYhKey,            // handle to key

  LPCTSTRlpValueName   //value name

);

參數說明

  • hKey:   打開鍵的句柄或以下預定義句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpValueName:   鍵值的名稱

返回值:If the function succeeds, the return value is ERROR_SUCCESS

讀取鍵值 RegQueryValueEx

函數原型

LONG RegQueryValueEx(

  HKEYhKey,            // handle to key

  LPCTSTRlpValueName,  //value name

  LPDWORDlpReserved,   //reserved

  LPDWORD lpType,       // type buffer

  LPBYTElpData,        //data buffer

  LPDWORDlpcbData      //size of data buffer

);

參數說明

  • hKey:   打開鍵的句柄或以下預定義句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpValueName:   鍵值的名稱
  • Reserved:   保留位,置0
  • lpType:   接收鍵值的類型
  • lpData:   接收鍵值
  • lpcbData:   接收鍵值數據長度
  1 #include "stdafx.h"
  2 #include <windows.h>
  3 #include <iostream>
  4 using namespacestd;
  5  
  6 int main()
  7 {
  8      HKEY hKey;    
  9      LPCTSTR lpRun =L"Software\\_MyTest";
 10      DWORD state,dwtype,sizeBuff;
 11      long lRet;
 12      char reBuff[10] ={0};
 13  
 14      //lRet =RegCreateKeyEx(HKEY_CURRENT_USER,lpRun,0,NULL,0,0,NULL,&hKey,&state);
 15      //if(lRet == ERROR_SUCCESS)
 16      //{
 17      //    if(state ==REG_CREATED_NEW_KEY)
 18      //       cout<<"表項創建成功"<<endl;
 19  
 20      //
 21      //    //關閉鍵
 22      //   RegCloseKey(hKey);
 23      //}
 24      //else if (state == REG_OPENED_EXISTING_KEY)
 25      //{
 26      //   cout<<"表項已存在"<<endl;
 27      //}
 28  
 29      //lRet = RegDeleteKey(HKEY_CURRENT_USER,lpRun);
 30      //if (ERROR_SUCCESS == lRet)
 31      //{
 32      //   cout<<"刪除鍵成功"<<endl;
 33      //}
 34      //else
 35      //   cout<<"刪除鍵失敗"<<endl;
 36  
 37      lRet = RegOpenKeyEx(HKEY_CURRENT_USER,lpRun,0,KEY_ALL_ACCESS,&hKey);
 38      if (ERROR_SUCCESS==lRet)
 39      {
 40          cout<<"打開鍵成功"<<endl;
 41      }
 42      else
 43          cout<<"打開鍵失敗"<<endl;
 44  
 45      LPCTSTR KeyName= L"KeyName";
 46      char KeyValue[20];
 47      DWORD type;
 48      DWORD len = sizeof(KeyValue);
 49      lRet = RegQueryValueEx(hKey,KeyName,0,&type,(BYTE*)KeyValue,&len);
 50      if (ERROR_SUCCESS==lRet)
 51      {
 52          cout<<"查詢鍵值成功------"<<KeyValue<<endl;
 53      }
 54      else
 55          cout<<"查詢鍵值失敗"<<endl;
 56  
 57  //  LPCTSTR KeyName =L"KeyName";
 58      //char KeyValue[] = "KeyValue";
 59      //lRet = RegSetValueEx(hKey,KeyName,0,REG_SZ,(BYTE*)KeyValue,20);
 60      //if (ERROR_SUCCESS == lRet)
 61      //{
 62      //   cout<<"寫入鍵值成功"<<endl;
 63      //}
 64      //else
 65      //   cout<<"寫入鍵值失敗"<<endl;
 66  
 67      //LPCTSTR KeyName = L"KeyName";
 68      //lRet = RegDeleteValue(hKey,KeyName);
 69      //if (ERROR_SUCCESS == lRet)
 70      //{
 71      //   cout<<"刪除鍵值成功"<<endl;
 72      //}
 73      //else
 74      //   cout<<"刪除鍵值失敗"<<endl;
 75  
 76      return 0;
 77 }
 78  ===================================添加開機啟動項============================================
 79 bool AutoStart()
 80 {
 81     //程序路徑
 82     char strMyPath[MAX_PATH];
 83     int retGMFN=GetModuleFileName(NULL,strMyPath,MAX_PATH);
 84     if (retGMFN==0)
 85     {
 86         return false;
 87     }
 88 
 89     //寫注冊表,達到開機啟動的效果
 90     HKEY    keyAutoStart;
 91     char    subKeyName[100]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
 92     LONG retROK=RegOpenKeyEx(HKEY_LOCAL_MACHINE,subKeyName,0,KEY_ALL_ACCESS,&keyAutoStart);
 93     if (retROK!=ERROR_SUCCESS)
 94     {
 95         int erro =GetLastError();
 96         return false;
 97     }
 98     //添加啟動項
 99     char keyValueName[100]="YunYaoHuLian_UpdateServer";
100     LONG retRSV=RegSetValueEx(keyAutoStart,keyValueName,0,REG_SZ,(BYTE *)strMyPath,strlen(strMyPath));
101     if (retRSV!=ERROR_SUCCESS)
102     {
103         int erro =GetLastError();
104         return false;
105     }
106 
107     //不用就關閉
108     RegCloseKey(keyAutoStart);
109 
110     return true;
111 }

 


免責聲明!

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



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