consider the following code
addHash("hash");
bool addHash(char* hash) {
HKEY hKey = 0;
int code = RegOpenKey(HKEY_CURRENT_USER, subkey, &hKey);
const int length = strlen(hash)+1;
WCHAR whash[100];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, hash, strlen(hash), whash, 100);
LONG setRes = RegSetValueEx(hKey, L"hash", 0, REG_SZ, (LPBYTE)whash, strlen(hash)+1);
return true;
}
After code is compiled and executed “ha” is puted into registry. Can somebody tell me where the problem is?
Thank you in advance!
The last argument is the number of bytes, not the number of characters, that the second last argument points to.
So the first five bytes (
strlen(hash) + 1) ofwhashwill be stored in the registry. Change to:You may also need to initialise
whash(I don’t thinkMultiByteToWideChar()adds a null terminator for you):