tags:

views:

68

answers:

2

I am having dumb monday so my apologies for posting such a newbie-like question.

I am using CRegKey.QueryValue to return a dword value from the registry. QueryValue writes the value into void* pData and the length into ULONG* pnBytes.

Now there is a way of getting it from pData into a wstring probably via stringstream. The closest I came was getting the result as a hex string. I was about to work on converting the hex representation to a dword and then from there to a wstring when I decided that was just dumb and ask on here instead of wasting another hour of my life on the problem.

+2  A: 

Why don't you use CRegKey::QueryDWORDValue instead? Then you could just swprintf_s to print it into a string (if you wish to).

humbagumba
How would I know that it was a DWORD ahead of time?
graham.reeds
If you know the name of the key, you should probably also know the data type.
Alan
If you don't know, then you'll have to use CRegKey::QueryValue, check if the value type is REG_DWORD, and then you can just cast the pData member into a DWORD e.g. DWORD x = *reinterpret_cast<DWORD*>(pData);
humbagumba
And then convert the dword with: `std::wstringstream ss; ss << value; return ss.str();`
graham.reeds
+1  A: 

I didn't test but should be fine:

/* To test if the value is REG_DWORD and get it */
DWORD dwValue;
switch (key->QueryDWORDValue(lpName, &dwValue)) {
    case ERROR_SUCCESS:
        cout << "All ok, value: " << dwValue;
        break;

    case ERROR_INVALID_DATA:
        cout << "Error, not DWORD value";
        break;

    default:
        cout << "Some other error";
        break;
}

/* to obtain the type */
DWORD dwType;
if (key->QueryValue(lpName, &dwType, NULL, NULL) == ERROR_SUCCESS)) /* use 'dwType' here... */
adf88