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");  ;
             RegSetValueEx(hKey, value, 0, REG_SZ, (BYTE*)localAppData, (_tcslen(localAppData) + 1) );
            ::MessageBox(hwndParent, _T("Couldn't open"), _T(""), MB_OK);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

evilrixSenior Software Engineer (Avast)Commented:
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.
nbrunsAuthor Commented:
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.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

nbrunsAuthor Commented:
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.Registry settings after write
Looks like a Unicode issue. Add * sizeof(TCHAR) like this
RegSetValueEx(hKey, value, 0, REG_SZ, (BYTE*)localAppData, (_tcslen(localAppData) + 1) * sizeof(TCHAR));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nbrunsAuthor Commented:
chaau, you literally just made my day! It works! Thank you so much!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.