Windows API一日一練(65)RegQueryValueEx函數

來源:互聯網
上載者:User

上一次介紹怎麼樣儲存資料到註冊表裡,這次就需要從註冊表裡讀取資料出來了。在這個例子裡是讀取字串資料出來,主要調用函數RegQueryValueEx來實現。下面的例子裡就是先查詢索引值的長度,然後再讀取內容出來。
 
函數RegQueryValueEx聲明如下:
 
WINADVAPI
LONG
APIENTRY
RegQueryValueExA (
    __in HKEY hKey,
    __in_opt LPCSTR lpValueName,
    __reserved LPDWORD lpReserved,
    __out_opt LPDWORD lpType,
    __out_bcount_opt(*lpcbData) LPBYTE lpData,
    __inout_opt LPDWORD lpcbData
    );
WINADVAPI
LONG
APIENTRY
RegQueryValueExW (
    __in HKEY hKey,
    __in_opt LPCWSTR lpValueName,
    __reserved LPDWORD lpReserved,
    __out_opt LPDWORD lpType,
    __out_bcount_opt(*lpcbData) LPBYTE lpData,
    __inout_opt LPDWORD lpcbData
    );
#ifdef UNICODE
#define RegQueryValueEx RegQueryValueExW
#else
#define RegQueryValueEx RegQueryValueExA
#endif // !UNICODE
 
hKey是主鍵。
lpValueName是索引值名稱。
lpType是類型。
lpData是讀出來資料儲存地方。
lpcbData是讀取資料多少。
 
調用函數的例子如下:
#001 //打註冊表傳回值。HKEY_CURRENT_USER/"Software"/"Wincpp"/"testreg"
#002  // /"Windows"//"winsize" = "800*600"
#003  //蔡軍生 2007/11/05 QQ:9073204 深圳
#004  std::wstring GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
#005         LPCTSTR lpszDefault)
#006  {
#007         //開啟應用程式鍵。
#008         HKEY hAppKey = GetAppRegistryKey();
#009         if (hAppKey == NULL)
#010         {
#011               return lpszDefault;
#012         }   
#013
#014         HKEY hSecKey = NULL;
#015         DWORD dw;
#016
#017         //開啟子鍵。
#018         RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
#019               REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
#020               &hSecKey, &dw);
#021         RegCloseKey(hAppKey);
#022
#023         if (hSecKey == NULL)
#024         {
#025               return lpszDefault;
#026         }   
#027
#028         //查詢索引值。
#029         std::wstring strValue;
#030         DWORD dwType=REG_NONE;
#031         DWORD dwCount=0;
#032
#033         //先查詢索引值的長度。
#034         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
#035               NULL, &dwCount);
#036         if (lResult == ERROR_SUCCESS)
#037         {        
#038               strValue.resize(dwCount);
#039
#040               //查詢索引值。
#041               lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
#042                    (LPBYTE)strValue.data(), &dwCount);
#043              
#044         }
#045
#046         RegCloseKey(hSecKey);
#047         if (lResult == ERROR_SUCCESS)
#048         {        
#049               return strValue;
#050         }
#051
#052         return lpszDefault;      
#053  }

相關文章

聯繫我們

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