RegQueryValueEx REG_SZ [C++]

本秂侑毒 提交于 2019-12-25 01:36:41

问题


which datatype should the variable that recives the data from the Registery have?

HKEY hKey;
HKEY hKey2;

DWORD dwMHz = MAX_PATH;
string pName;


long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
        0,
        KEY_READ,
        &hKey);
long lError2 = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
        0,
        KEY_READ,
        &hKey2);

// Working
RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);

//Not working                                            <-- THIS -->
RegQueryValueEx(hKey2, "ProcessorNameInfo", NULL, NULL, (LPBYTE) &pName, &BufSize2);


cout << "   Processor frequency: " << dwMHz / 1024 << " GHz" << endl;
cout << "   Processor Name: " << pName << endl;

RegCloseKey(hKey);
RegCloseKey(hKey2);

When I try to print the 'pName' I get nothing.


回答1:


aYou'll want to read this data into an array of type TCHAR, which depending on how your application is built (UNICODE/MBCS) will be properly typed as char or wchar_t.

When reading REG_SZ strings using this API, you need to make sure of 2 things.

1) Your output buffer needs to have space for any trailing NULL terminator 2) On success, your output buffer MIGHT NOT BE NULL TERMINATED.

That last bit is important - if the string was not stored with a NULL terminator, then your output buffer won't be either. You need to watch for this and handle this properly, lest you end up with a security vulnerability in your application.



来源:https://stackoverflow.com/questions/12125069/regqueryvalueex-reg-sz-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!