我们在调用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;
RegQueryValueEx(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。由此来说,我们在调用这个函数之前,要保证最后一个参数的值要大于等于读取的数据的长度,执行完该函数后,这个参数的值为实际的长度
本文详细介绍了在使用RegQueryValueEx函数读取注册表数据时遇到234错误的常见原因及解决方案。通过实例演示了如何确保缓冲区大小足够以避免错误,并提供了代码示例来帮助开发者正确处理这种情况。
4145

被折叠的 条评论
为什么被折叠?



