Search code examples
c++registry

c++ add to registry


So, the code is barking at me, need a bit of help. Error: char * is incompatible with cost BYTE *. It's barking at: RegSetValueExA(hKey, "My App", 0, REG_SZ, buffer, sizeof(buffer)); specifically the buffer.

Error message(vstudio): Error 3 error C2664: 'RegSetValueExA' : cannot convert parameter 5 from 'char [500]' to 'const BYTE *' {snip}\visual studio 2010\projects\reg\reg\reg.cpp 74

My code (note: stemres = full path the executable, in LPCWSTR, which I convert successfully to char):

HKEY hKey;
RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);
// stemres = the executable path
char buffer[500];
wcstombs(buffer,stemres,500);
RegSetValueExA(hKey, "My App", 0, REG_SZ, buffer, sizeof(buffer));
RegCloseKey(hKey);
cout << "Added to registry.";

Solution

  • The fifth parameter for RegSetValueExA() is const BYTE * which should be synonym for const unsigned char * and you try to pass char * to it. I am afraid you have to convert it:

     RegSetValueExA(hKey, "My App", 0, REG_SZ, reinterpret_cast<const BYTE *>(buffer), sizeof(buffer));