请问RegQueryValueEX这个函数读REG_SZ值是乱码是怎么回事

mortimer7866 2015-11-11 07:41:13
在最后一步输出的时候程序停止运行,单步调试发现读出的data中是乱码,请大神帮忙解决
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;

string GetVal(HKEY hKey, LPCTSTR lpValue) //读取当前注册表键值
{
string data;
DWORD size = 200;
DWORD type = REG_SZ;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);

if (nError == ERROR_FILE_NOT_FOUND)
data = "file not found"; // The value will be created and set to data next time SetVal() is called.
else if (nError)
return "read error";
/*cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;*/

return data;
}

int main()
{
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}\\0000", 0, KEY_WOW64_64KEY | KEY_ALL_ACCESS, &hKey);
cout<<GetVal(hKey, L"DriverDesc")<<endl;
}
...全文
388 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-03-22
  • 打赏
  • 举报
回复
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
赵4老师 2016-03-22
  • 打赏
  • 举报
回复
RegQueryValueEx The RegQueryValueEx function retrieves the type and data for a specified value name associated with an open registry key. 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 ); …… lpcbData Pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the lpData parameter. When the function returns, this variable contains the size of the data copied to lpData. If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then lpcbData will also include the size of the terminating null character. The lpcbData parameter can be NULL only if lpData is NULL. If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into the variable pointed to by lpcbData. If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This lets an application determine the best way to allocate a buffer for the value's data.
赵4老师 2016-03-22
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
KTs_3 2016-03-21
  • 打赏
  • 举报
回复
我先在遇到了和楼主一样的问题,也是读取到的REG_SZ数据是乱码。。。请问你解决这个问题了么?求助啊~
赵4老师 2015-11-12
  • 打赏
  • 举报
回复
请判断每个函数的返回值。
mortimer7866 2015-11-11
  • 打赏
  • 举报
回复
引用 2 楼 iyomumx 的回复:
要读多长的数据,就new多少个char出来,不要用string
能不能麻烦大侠帮忙调试一下程序
mortimer7866 2015-11-11
  • 打赏
  • 举报
回复
引用 2 楼 iyomumx 的回复:
要读多长的数据,就new多少个char出来,不要用string
对RegQueryValueEX不太会用,我用这个函数size设为定值13读取mac地址可以成功,但是现在我要读的数据不是定值,也没办法每次去new
iyomumx 2015-11-11
  • 打赏
  • 举报
回复
要读多长的数据,就new多少个char出来,不要用string
mortimer7866 2015-11-11
  • 打赏
  • 举报
回复
程序需要管理员权限,UAC执行级别里面需要设为require administrator,RegQueryValueEX这个函数返回错误代码234,经过查阅MSDN得出是data长度不够的问题,但是我怎么改都无法正常运行,求大侠帮忙

69,393

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧