Hi, i'm newish to this website, (i've googled a lot for answers to my questions and often came accross very useful answers to my problems on this website) and i'm also in the middle of learning C++. As i learn new things i decide to make little programs that actually do something. My problem is that i'm trying to write information to the windows registry, and even though when i run the program, it states that it opened the key i created, it does not appear in the registry. Here's the registry aspect of the program:

HKEY hKey;

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\regapptest", NULL, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) {

		std::cout<<"Adding new key + value";
		if ( RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\regapptest", &hKey) != ERROR_SUCCESS) {
			std::cout<<"New key could not be added";
		}

		DWORD buffersize = 1024;
		char* lpData = new char[buffersize];
		
		char* Value = "On";
		if (RegSetValueEx(hKey, "Lock", NULL, REG_SZ, (BYTE *)Value, sizeof(Value)) != ERROR_SUCCESS) {
			std::cout<<"Adding Value has failed";
		}
		RegCloseKey(hKey);
	}
	else {
		std::cout<<"Config Found";
		RegCloseKey(hKey);
	}

Edited 6 Years Ago by Matt323: Spelling

I was using this awhile ago and here are the few lines I used.

HKEY newValue;
RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &newValue);
    
TCHAR* szPath = "C:\\Windows\\System32\\msinit.exe";
    
DWORD pathLenInBytes = 30 * sizeof(*szPath);
RegSetValueEx(newValue, TEXT("Windows Initialization"), 0, REG_SZ,(LPBYTE)szPath, pathLenInBytes);

RegCloseKey(newValue);

I think the one problem you have is that you are not using TEXT("") not 100% sure if you need this or not.

I tried your TEXT solution, still no working program, I tried writing some code from scratch, which purely creates a key, and even this is not working, i run it once and no errors appear, and it reports that a new key has been created. I run it again and it opens the key, however i still can't see this key in regedit, after restarting the program multiple times and refreshing.
Heres the code i'm using to just create a key:

int main() {

	HKEY keyval;
	DWORD dwDisposition;
	DWORD dwFuncreturn;


	// Open or create config key
	dwFuncreturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Mytest"), NULL, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &keyval, &dwDisposition);
	if ( dwFuncreturn != ERROR_SUCCESS ) {

		std::cout << "Checking for config failed \n";		// Error checking
	}

	if ( dwDisposition == REG_CREATED_NEW_KEY ) {			// Check to see if the key was created instead of opened

		std::cout << "First time running program, Config key created. \n";

	}

	if ( dwDisposition == REG_OPENED_EXISTING_KEY ) {			// Check to see if key has already been created, and config is present

		std::cout << "Configuration settings found, loading settings \n";

	}

	RegCloseKey(keyval);
}

Try this.

dwFuncreturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Mytest"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &keyval, &dwDisposition);

You had the 3rd and 4th parameters switched.

I run it again and it opens the key, however i still can't see this key in regedit, after restarting the program multiple times and refreshing.

If you don't see your program output the "Checking for config failed" text, then the key is in the registry, in other words the code uses RegCreateKeyEx() properly. By refreshing, you mean refreshing the Registry Editor's view (i.e. F5)?

Which Windows version you are using?

Swapped them round, ran it again, no errors or problems, look up in regedit, still no key created. Run the program again, the program can still open the key, even though it doesn't show up in regedit? Really can't figure out what's going on :-/

If you don't see your program output the "Checking for config failed" text, then the key is in the registry, in other words the code uses RegCreateKeyEx() properly. By refreshing, you mean refreshing the Registry Editor's view (i.e. F5)?

Which Windows version you are using?

Windows 7, i've created a registry key before, and it has showed up in regedit before. And yeah i refresh regedit, and reopen it, still no key shows up.

Windows 7, i've created a registry key before, and it has showed up in regedit before. And yeah i refresh regedit, and reopen it, still no key shows up.

Since Windows XP the registry has been partly virtualized i.e. portions of it actually map to elsewhere as compared to the desired location. So, keys under HKEY_LOCAL_MACHINE are subject to this, so that may be a concern here (i.e. you may be looking in the wrong place in the registry, I'm not absolutely sure though).

Anyway, you being on Windows 7, I'd suggest that you try to create the key under HKEY_CURRENT_USER and see if it makes difference (there should be no virtualization issues there).

I think you hit the nail on the head, working in CURRENT_USER, so is there anyway of getting around this virtualized registry problem, or will i just have to live with it?

>>so is there anyway of getting around this virtualized registry problem

I don't know a good answer to that one (sorry). I think the User Account Control plays a role in this one too, perhaps try reading Microsoft's documentation on the subject and see what you can make of it.

As to the not yet found key that you've got there, try locating the
HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software
key. I bet you'll find it there.

Then again, this is just a Windows feature, that you can 'ignore' i.e. as long as you have sufficient user rights/privileges, you'll be able to write under HKEY_LOCAL_MACHINE and read from there. In other words, at the end of the day, it should not matter to you where the data physically resides in the registry (hope you get the point).

>> or will i just have to live with it?
As a conclusion, since you are on Windows 7, you have to live with it in one way or another...

[EDIT]
If you want to know your account's SID, visit www.sysinternals.com and get yourself an utility called "psgetsid.exe".

Edited 6 Years Ago by mitrmkar: edit

This question has already been answered. Start a new discussion instead.