nbruns
asked on
RegSetValueEx not working
I am not a c++ programmer, but had to create a CE Setup DLL to modify an installation on a Windows Mobile 6.5 handheld device. I am trying to update a registry key with a specific path. Everything is working except the write to the registry. If I check the return value of RegSetValueEx, it is 0, but the registry key is not set. I appreciate any help!
HRESULT hr;
TCHAR localAppData[MAX_PATH];
SHGetSpecialFolderPath(0,l ocalAppDat a, CSIDL_PROGRAMS,0);
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MA CHINE,_T(" Software\\ J_s\\J_e\\ Apps"),0, KEY_ALL_ACCESS, &hKey);
if (openRes==0) {
LPCTSTR value = TEXT("App4");
TCHAR newValue[] = _T("V Prog.lnk"); ;
_tcscat_s(localAppData,260 ,newValue) ;
RegSetValueEx(hKey, value, 0, REG_SZ, (BYTE*)localAppData, (_tcslen(localAppData) + 1) );
RegCloseKey(hKey);
}
else
{
::MessageBox(hwndParent, _T("Couldn't open"), _T(""), MB_OK);
}
HRESULT hr;
TCHAR localAppData[MAX_PATH];
SHGetSpecialFolderPath(0,l
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MA
if (openRes==0) {
LPCTSTR value = TEXT("App4");
TCHAR newValue[] = _T("V Prog.lnk"); ;
_tcscat_s(localAppData,260
RegSetValueEx(hKey, value, 0, REG_SZ, (BYTE*)localAppData, (_tcslen(localAppData) + 1) );
RegCloseKey(hKey);
}
else
{
::MessageBox(hwndParent, _T("Couldn't open"), _T(""), MB_OK);
}
As well as the previous point (and the code is too incomplete to know whether that is or is not relevant)...
You don't check the result of RegOpenKeyEx so how do you know you've successfully opened the key?
You don't check the result of RegSetValueEx so how do you know you've successfully written the value?
Keys in HKLM tend to be protected and require the process to have elevated privileges to be able to modify values.
I suspect if you actually check the results of the functions you are calling (and use GetLastError() to get more detail) you'll find out exactly why this isn't working for you.
You don't check the result of RegOpenKeyEx so how do you know you've successfully opened the key?
You don't check the result of RegSetValueEx so how do you know you've successfully written the value?
Keys in HKLM tend to be protected and require the process to have elevated privileges to be able to modify values.
I suspect if you actually check the results of the functions you are calling (and use GetLastError() to get more detail) you'll find out exactly why this isn't working for you.
ASKER
I actually do have the code as
LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MA CHINE,_T(" Software\\ J_s\\J_e\\ Apps"),0, KEY_ALL_ACCESS, &hKey); .
I had removed the declaration when I posted, for brevity, but I guess I should have left it complete. I think the problem may actually be the \ characters in the path I am writing to the registry. I noticed that the line TCHAR newValue[] = _T("\V Prog.lnk") needs to have the \ changed to \\. How can I add the extra "\" to the path that is being returned in localAppData?
Thanks for your help! C++ is driving me crazy.
LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MA
I had removed the declaration when I posted, for brevity, but I guess I should have left it complete. I think the problem may actually be the \ characters in the path I am writing to the registry. I noticed that the line TCHAR newValue[] = _T("\V Prog.lnk") needs to have the \ changed to \\. How can I add the extra "\" to the path that is being returned in localAppData?
Thanks for your help! C++ is driving me crazy.
Use PathCombine function for this. It takes care of the situation when the input path already has a \ at the end and combines the path for you. Instead of writing _tcscat_s(localAppData,260 ,newValue) ; use this:
PathCombine(localAppData, localAppData, newValue);
ASKER
I now have the correct value to write to the registry (shown using messagebox) and it does write to the registry, but it only writes part of the value with some kind of non-printable character at the end. Is there a problem with the length of localAppData maybe? I have attached an image showing the value written. It is on the fifth line.
Thanks for all your help! I really appreciate it.
Thanks for all your help! I really appreciate it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
chaau, you literally just made my day! It works! Thank you so much!!
Open in new window
Your openRes variable is not initailised and take an arbitrary value (usually something like 246234762, or similar). When you test it using "if (openRes==0) " it will never go to the next scope.What you need to do is to assign this variable:
Open in new window