Help with registry startup key code

I am trying to set a run key for my application so that it will be loaded when Windows starts. Everything is working except for the fact that the value of the new string I create under the run key seems to cut off at 32 characters. For example, if I tried to set it to c:\Users\MichaelW\AppData\Roaming\Custom\application.exe it shows up as c:\Users\MichaelW\AppData\Roaminin. Also, I did not include the code to get the rootDrive (e.g. C:) and current user but I can if need be. I am very new to c++ so any help would be appreciated.



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
	std::string runKeyName = "Custom";							
	HKEY hkey = NULL;									
	LONG res = NULL;										
	LPCSTR name = const_cast<char *>(runKeyName.c_str());
	std::string HKCUPath = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
	std::string combined = HKCUPath + "\\" + runKeyName;
	LPCSTR fullPathRegKey = const_cast<char *>(combined.c_str());	// used in RegQueryValueEx
	DWORD type = REG_SZ;											// used in RegQueryValueEx
	DWORD val = NULL;												// used in RegQueryValueEx
	DWORD cbData = 1024;											// used in RegQueryValueEx



	const string Path = rootDrive + "\\Users\\" + userName + "\\AppData\\Roaming\\Custom\\application.exe";

	// check if the run key exists
	res = RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hkey);
	
	if (res == ERROR_SUCCESS)
	{

		// Attempt to create the subkey
		RegSetValueExA(hkey, name, 0, 1, (const BYTE *) Path.c_str(), sizeof(Path));


		//res = RegQueryValueExA(hkey, fullPathRegKey, NULL, &type, (LPBYTE)&val, &cbData);


		// close the registry key
		RegCloseKey(hkey);	
        }
Last edited on
sizeof(Path) == sizeof(std::string) (32 in this implementation)
Path.size() == number of characters that the string holds

1
2
// RegSetValueExA(hkey, name, 0, 1, (const BYTE *) Path.c_str(), sizeof(Path));
RegSetValueExA( hkey, name, 0, REG_SZ, (const BYTE *) Path.c_str(), Path.size() + 1 ); // +1 for the terminating null character 
The problem is this:

RegSetValueExA(hkey, name, 0, 1, (const BYTE *) Path.c_str(), sizeof(Path));

Use size() instead:

RegSetValueExA(hkey, name, 0, 1, (const BYTE *) Path.c_str(), Path.size());
Thanks, that did it. Appreciate the quick responses.
Topic archived. No new replies allowed.