Working with the Registry

Hello,

I have been trying to write a simple program that will read and edit the registry. It seems to open the keys fine, but then when it goes to write, nothing is written in the registry. The program is run as administrator, so it isn't a lack of privilege.

The purpose of the program is to setup automatic logon for an account on multiple computers (the multiple computers part is already handled elsewhere). The values are going to be reset (set to empty strings) when work on the computers is done, and then rebooted to remove the automatic logon. However, I am having trouble reading (which I pretty much gave up on doing, since it's not required) and writing. I receive no error attempting to open the registry key.


The code I'm attempting to use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <windows.h>
#include <malloc.h>
#include <stdio.h>

int main(int argc, char* argv[]){
  if(argc < 2){
    printf("Usage: %s on/off [username] [password] [domain]", argv[0]);
    return 1;
  }
  int ecode = 0;
  HKEY hkey = 0;
  LPCTSTR skey = TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon");
  if((ecode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,         // HKEY
                           skey,                       // Sub Key
                           0,                          // reserved
                           KEY_ALL_ACCESS,             // Access flags
                           &hkey)) != ERROR_SUCCESS){  // Returned HKEY
    printf("Error opening registry key: %i\n", ecode);
    return 1;
  }
  LPCTSTR data = TEXT("Data\0");
  int length = (strlen(data)*sizeof(LPCTSTR));
  if((ecode = RegSetValueEx(hkey,                       // Returned HKEY
                            TEXT("DefaultPassword"),    // Value
                            0,                          // Reserved
                            REG_SZ,                     // Type
                            (LPBYTE)data,               // Data
                            length)) != ERROR_SUCCESS){ // Size of data
    printf("Error setting value: %i", ecode);
    return 2;
  }
  if(argv[1] == "on"){

  } else if(argv[1] == "off"){

  }
}


There are no errors on opening nor editing the registry, yet when I refresh regedit, there is no change.

This compiles fine on CodeBlocks 13.12, and I have tried adding '-ladvapi32' to the build options for the project.

Any help to solve my issue is greatly appreciate,

~ Zalerinian
Last edited on
You're not writing the zero terminator on the string. Do you need to?
Last edited on
What do you mean I'm not writing the zero terminator in the string? It's there at LPCTSTR data = TEXT("Data\0");, isn't it? Doesn't that add the Null Termination Character (what I assume you mean by zero termination character) to the end of the string, then convert that whole thing to an LPCTSTR?
int length = (strlen(data)*sizeof(LPCTSTR));
Changing this to add one did not have any effect, there is still no change to the registry.
You don't add 1, you add 1*sizeof(TCHAR), which is probably 2.
Try this:

int length = ((strlen(data) + 1) * sizeof(*data)); // sizeof(LPCTSTR) is the size of the pointer (maybe 4)

[EDIT]
LPCTSTR data = TEXT("Data"); // This contains already the terminating 0
[/EDIT]
Last edited on
The code exactly as posted "works" for me - windows 7 x86, running as administrator. It creates "DefaultPassword" entry with value "Data" in SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon hive.

Aside from that and unrelated to it, this snippet does not do what you want:
1
2
3
4
5
if(argv[1] == "on"){

  } else if(argv[1] == "off"){

  }


It invokes undefind behaviour and you should use strcmp() instead.
That's odd, I'm testing this on Windows 8.1 and it won't change the value. I tested on a new install of Windows 7 Pro 64-bit, and the value still did not change. On the windows 7 machine, the entry is not even created. I ran the program as administrator (right click, run as administrator) on both machines, but no luck.
I ran the program as administrator (right click, run as administrator) on both machines, but no luck.
Yes, it's a rights problem. Running it as admin isn't enough. You need to start the regedit and change rights there. Even though it uses the rights it seems to be independent.
It is the program also 64 bit if you are running a 64 bit operating system ?

On my machine (win 7 x86 runs without problems -> run as administrator without changing anything with regedit)
Last edited on
my account has full permission to access that registry key, and I run the program as an administrator, yet no changes are made. I don't know what could possibly be the issue. Here's an imgur album with images and descriptions to show what I have: http://imgur.com/a/l99RA

Edit:
And the code I used in the images above: http://i.imgur.com/Nds9dRf.png
Last edited on
Registered users can post here. Sign in or register to post.