Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do with these old-style casts?

Tags:

c++

casting

I'm getting a few "use of old-style cast" warnings which I would like to get rid of, but I don't know enough about this.

Edit: HKEY_CURRENT_USER is indeed defined in the WinAPI so I will leave that one alone.

(LPBYTE)&result: LPBYTE(&result) and reinterpret_cast<LPBYTE>(&result) work, but I have no idea if either is equivalent. So which of these three do I use?

(const BYTE*)&value: reinterpret_cast<const BYTE*>(&value) works, but same thing again. So which of these two do I use?

Some more context:

HKEY hKey;
std::string sResult = "";
if(regOpenKey(KEY_READ, &hKey))
{
    DWORD size=1024, type = REG_SZ;
    wchar_t result[MAX_PATH];
    if(RegQueryValueEx(hKey, key, nullptr, &type, (LPBYTE)&result, &size) == ERROR_SUCCESS)
        sResult = str_narrow(result);
}

RegCloseKey(hKey);

and:

HKEY hKey;
if(regOpenKey(KEY_ALL_ACCESS, &hKey))
{
    DWORD value = 1;
    RegSetValueEx(hKey, key, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
}

RegCloseKey(hKey);

Thanks for any help :)

like image 797
bur Avatar asked Sep 26 '18 09:09

bur


1 Answers

HKEY_CURRENT_USER is defined in the WinAPI, so leave it alone. It could change without warning (although unlikely, but possible).

(LPBYTE)&result: LPBYTE(&result) works, but again, I have no idea if it's equivalent.

Yes, its the same.

(const BYTE*)&value: reinterpret_cast<const BYTE*>(&value) works, but same thing again.

The same again.

like image 145
Swordfish Avatar answered Oct 10 '22 18:10

Swordfish