Reputation: 424
I am working on one small issue and I want to set the Default Directory at runtime. so I have implemented the code below and still it is not working though it is not giving me any error.
I have wrote the code as specified below. please let me know what is the mistake in the code?
HKEY hKey;
LPCTSTR sk = TEXT("Software\\Microsoft\\Internet Explorer\\Main");
LPCTSTR value = TEXT("Default Download Directory");
LPCTSTR newValue = TEXT("C:\\Users\\USRNAME\\PROJ\\My Files");
LONG lRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk,0, KEY_READ, &hKey);
bool bExistsAndSuccess(lRes == ERROR_SUCCESS);
RegCloseKey(hKey);
if (bExistsAndSuccess)
{
MessageBox(NULL, ptr, _T("bingo, Found you & key"), MB_OK | MB_ICONINFORMATION);
if (RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)newValue, sizeof(newValue)+1) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
MessageBox(NULL, ptr, _T("bingo, success"), MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBox(NULL, ptr, _T("bingo, failure"), MB_OK | MB_ICONINFORMATION);
}
}
else
{
MessageBox(NULL, ptr, _T("bingo, Found you without key"), MB_OK | MB_ICONINFORMATION);
}
It shows me bingo, success message box but value is not updating in the registry. please help me.
Upvotes: 1
Views: 434
Reputation: 424
As molbdnilo mentioned, newValue is a pointer. sizeof(newValue) is not the length of the string. so I have changed the code with _tcslen(newValue) * sizeof(TCHAR)+1
& as per the comments of Vlad, I have removed to RegCloseKey
statement which is executed before RegSetValueEx
Upvotes: 1