Link to home
Start Free TrialLog in
Avatar of nbruns
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,localAppData, CSIDL_PROGRAMS,0);
      
      HKEY hKey;      
      RegOpenKeyEx(HKEY_LOCAL_MACHINE,_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);
      }
Avatar of chaau
chaau
Flag of Australia image

Your problem is at this line:
 RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\J_s\\J_e\\Apps"),0, KEY_ALL_ACCESS, &hKey);

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:
 openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\J_s\\J_e\\Apps"),0, KEY_ALL_ACCESS, &hKey);

Open in new window

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.
Avatar of nbruns
nbruns

ASKER

I actually do have the code as
LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,_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.
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);

Open in new window

Avatar of nbruns

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.User generated image
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nbruns

ASKER

chaau, you literally just made my day! It works! Thank you so much!!