Click here to Skip to main content
15,887,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a small program which insert the value and its corresponding value into the Windows registry key.

Program is working fine but it is not inserting value and its corresponding value.

And one more thing when I run prog as an administrator RegSetValueEx() fails..but still inssert only the value not its data.

Please help for finding out the issue here.

My code is as follows..



C
#define WIN32_LEAN_AND_MEAN
#define WIN32_DEFAULT_LIBS
#ifndef _WIN32_WINNT
#define _WIN32_WINNT (0x0601)
#endif  /* _WIN32_WINNT */
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <tchar.h>
#include <unistd.h>
#include <stdbool.h>

#include<string.h>

    BOOL InstallRunOnStartup()
    {
      HKEY key;

      long result;
      BOOL ret = FALSE;
      LPTSTR val=L"12as3d12";
        LPTSTR a=L"zzz";

      TCHAR szBuf[20];


      result = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", NULL, KEY_WRITE | KEY_WOW64_64KEY | KEY_SET_VALUE , &key);
      if (result == ERROR_SUCCESS)
      {

        printf("hi \n");
          if (RegSetValueEx(key, a, 0, REG_SZ,(LPBYTE)val, (DWORD)(lstrlen(val)+1) == ERROR_SUCCESS)){
            printf("success \n");
            ret = TRUE;
          }
            RegCloseKey(key);
      }
      return ret;
    }




    int main()
    {
        InstallRunOnStartup();
        getch();
    }
Posted
Updated 26-Sep-13 22:13pm
v2

1 solution

You are mixing Unicode and ANSI:

With ANSI builds, you are assigning Unicode strings to char* for your val and a variables.

With Unicode builds, you are passing an ANSI string to RegOpenKeyEx().

With ANSI builds, you are passing Unicode strings for value name and value to RegSetValueEx().

With Unicode builds, the RegSetValueEx() cbData parameter is wrong (the total length must be multiplied with sizeof(WHACR)).

Solution:
Use Unicode setting dependend macros and functions:
LPTSTR val=_T("12as3d12");
LPTSTR a=_T("zzz");
RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run)", // ...
RegSetValueEx(key, a, 0, REG_SZ,(LPBYTE)val, (DWORD)(_tcslen(val)+1) * sizeof(TCHAR));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900