RegQueryValueEx返回234錯誤解決方法

我們在調用RegQueryValueEx這個函數,在註冊表中讀取字符串形式的數據時候,經常會出現返回234錯誤。 MSDN說是 If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the value ERROR_MORE_DATA, Window NT: If hKey specifies HKEY_PERFORMANCE_DATA and the lpData buffer is too small, RegQueryValueEx returns ERROR_MORE_DATA but lpcbData does not return the required buffer size. This is because the size of the performance data can change from one call to the next. In this case, you must increase the buffer size and call RegQueryValueEx again passing the updated buffer size in the lpcbData parameter. Repeat this until the function succeeds. You need to maintain a separate variable to keep track of the buffer size, because the value returned by lpcbData is unpredictable.

解決方法如下:

BYTE byBuffer[1024];

 DWORD dwLen = 1024;

// 讀取數據庫密碼 RegQueryValueEx(m_hKey, "DBPassword", NULL, &dwType, byBuffer, &dwLen); m_pt.strPwd = (LPCTSTR)byBuffer;

// 讀取數據庫服務器地址

dwLen = 1024; R

egQueryValueEx(m_hKey, "DBServerAddress", NULL, &dwType, byBuffer, &dwLen);

 m_pt.strHostName = (LPCTSTR)byBuffer;

 

// 讀取數據庫用戶名 dwLen = 1024;

RegQueryValueEx(m_hKey, "DBUser", NULL, &dwType, byBuffer, &dwLen);

m_pt.strDBUser = (LPCTSTR)byBuffer;

 上述代碼中,當我們讀取完密碼字符串後,dwLen這個變量會變爲8,如果我們不做任何操作而去讀下面的服務器地址字符串時,很容易發生234錯誤。因爲服務器地址字符串的長度是10。由此來說,我們在調用這個函數之前,要保證最後一個參數的值要大於等於讀取的數據的長度,執行完該函數後,這個參數的值爲實際的長度

引用自:http://blog.csdn.net/liuliu20036/archive/2008/11/22/3351541.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章