C++ Registry Edit - c++

I am writing an application in c++ that will be executed on Windows Vista. I want to add a registry entery to start my app along with windows, but I got an error: permission denied. How do I bypass that. Here is my code:
void Persist::RunOnWindowsBoot()
{
HKEY hKey;
char ExeDir[MAX_PATH] = "E:\\Projects\\Coro\\Coro.exe";
RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey);
if(RegQueryValueEx(hKey, TEXT("System"), NULL, NULL, NULL, NULL) == ERROR_FILE_NOT_FOUND)
{
RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey);
RegSetValueEx(hKey, "System", 0, REG_SZ, (const unsigned char*)ExeDir, MAX_PATH);
RegCloseKey(hKey);
}
}

Related

Adding to the registry in C++ win32

I have a batch file that has this line..
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\SomeName" /ve /t REG_SZ /d "/path/to/SomeName.json" /f
I want to do the same from a c++ code. I found the function RegCreateKeyExA and I am using it like this
HKEY hKey;
LPCSTR sk = "Software\Google\Chrome\NativeMessagingHosts\SomeName";
LONG openRes = RegCreateKeyExA(
HKEY_CURRENT_USER,
sk,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
but how to add the data part? /ve /t REG_SZ /d "/path/to/SomeName.json" /f
UPDATE:
based on #Alan Britles comment I did the following:
HKEY hKey;
LONG openRes = RegCreateKeyExA(
HKEY_CURRENT_USER,
"HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\SomeName",
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
if (openRes == ERROR_SUCCESS) {
log+="Success creating key.";
}
else {
log += "Error creating key.";
}
LPCSTR value = NULL;
std::string temp = QString(data_path + "\\SomeName.json").toStdString();
LPCSTR data = temp.c_str()+'\0';
LONG setRes = RegSetValueExA(hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data) + 1);
if (setRes == ERROR_SUCCESS) {
log+="Success writing to Registry.";
}
else {
log += "Error writing to Registry.";
}
I get success Writing but I don't see it added in the registry editor... What am I doing wrong?
Update #2:
I got rid of HKCU in the sub key..
LONG openRes = RegCreateKeyExA(
HKEY_CURRENT_USER,
"Software\\Google\\Chrome\\NativeMessagingHosts\\SomeName",
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
But I still get success returns and nothing added to the registry.. Is it a problem related to permissions? Or am I calling the wrong functions?

How can i add registry key with c++ on HKEY_LOCAL_MACHINE [duplicate]

Could anybody tell me what's wrong is with this code? There is no errors. Everything returns ERROR_SUCCESS but in register can't see any changes.
void Utils::writePath(LPCTSTR data)
{
HKEY hkey;
DWORD dwDisposition;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\aaTestCompany\\testApp"),
0, NULL, 0,
KEY_WRITE, NULL,
&hkey, &dwDisposition) == ERROR_SUCCESS)
{
long setRes = RegSetValueEx (hkey, "testPath", 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if (setRes == ERROR_SUCCESS) {
printf("Success writing to Registry.");
} else {
printf("Error writing to Registry.");
}
RegCloseKey(hkey);
}
else
MessageBox(NULL,"error","",0);
}
As the application is 32-bit on a 64-bit OS the registry key will actually be created beneath HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node.
See 32-bit and 64-bit Application Data in the Registry.
Can you try with 5th and 6th parameters as REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS

C++ - RegCreateKeyEx success but without result

Could anybody tell me what's wrong is with this code? There is no errors. Everything returns ERROR_SUCCESS but in register can't see any changes.
void Utils::writePath(LPCTSTR data)
{
HKEY hkey;
DWORD dwDisposition;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\aaTestCompany\\testApp"),
0, NULL, 0,
KEY_WRITE, NULL,
&hkey, &dwDisposition) == ERROR_SUCCESS)
{
long setRes = RegSetValueEx (hkey, "testPath", 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if (setRes == ERROR_SUCCESS) {
printf("Success writing to Registry.");
} else {
printf("Error writing to Registry.");
}
RegCloseKey(hkey);
}
else
MessageBox(NULL,"error","",0);
}
As the application is 32-bit on a 64-bit OS the registry key will actually be created beneath HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node.
See 32-bit and 64-bit Application Data in the Registry.
Can you try with 5th and 6th parameters as REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS

How to set a value in windows registry? (C++)

I want to edit key "HKEY_LOCAL_MACHINE\Software\company name\game name\settings\value" to "1" (DWORD)
This is my code:
HKEY hkey;
DWORD dwDisposition;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\company name\\game name\\settings"), 0, NULL, 0, 0, NULL, &hkey, &dwDisposition) == ERROR_SUCCESS){
DWORD dwType, dwSize;
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
DWORD rofl = 1;
RegSetValueEx(hkey, TEXT("value"), 0, dwType, (PBYTE)&rofl, dwSize); // does not create anything
RegCloseKey(hkey);
}
But it doesnt do anything. RegCreateKeyEx() is the only function that actually does something: creates the "folders" in the registry only. So once again how im failing? How i can create "files" in the registry?
Always check the return value of API functions. You'll see that RegSetValueEx() returns 5, access denied. You didn't ask for write permission. Fix:
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("Software\\company name\\game name\\settings"),
0, NULL, 0,
KEY_WRITE, NULL,
&hkey, &dwDisposition) == ERROR_SUCCESS) {
// etc..
}
You probably need to pass in KEY_WRITE as the value of the samDesired arugment to RegCreateKeyEx() function (sixth argument).
This is a copy and edit from an actual code, may contain errors.
LONG
SetRegValue
(
const wchar_t* path
,const wchar_t *name
,const wchar_t *value
)
{
LONG status;
HKEY hKey;
status = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &hKey);
if ( (status == ERROR_SUCCESS) && (hKey != NULL))
{
status = RegSetValueEx( hKey, name, 0, REG_SZ, (BYTE*)value, ((DWORD)wcslen(value) + 1)*sizeof(wchar_t));
RegCloseKey(hKey);
}
return status;
}

Access is Denied for registry

I am playing with the registry programmatically for the first time, and it's not working that well (but at least I haven't destroyed my computer). Specifically, I keep getting back Error 5 (Access is Denied) from RegCreateKeyEx and RegSetValueEx. The thing that is strangest to me is that when HKEY_CURRENT_USER\Software\dir1\Sub Directory already exists, RegCreateKeyEx fails with Error 5, but when it doesn't already exist, it creates it successfully; and then fails on the RegSetValueEx.
Am I doing anything wrong in this code?
BOOL MyDialog::SaveLocationsToRegistry()
{
HKEY hkey;
DWORD dwDisposition;
DWORD dwType, dwSize;
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
0, NULL, 0, 0, NULL, &hkey, &dwDisposition);
if(result == ERROR_SUCCESS)
{
LPCTSTR szLastFolder = "C:\\Documents and Settings\\user\\My Documents\\Copy of item\0";
dwType = REG_SZ;
dwSize = strlen(szLastFolder)+1;
LONG setResult = RegSetValueEx(hkey, TEXT("LastFolder"), 0, dwType,
(PBYTE)&szLastFolder, dwSize);
RegCloseKey(hkey);
return setResult == ERROR_SUCCESS;
}
else
{
return false;
}
}
Note: The absolute path is only there temporarily. Baby steps ;-)
You're not asking for any access rights. You probably want to specify KEY_WRITE (or something) for the 6th parameter (samDesired).
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\dir1\\Sub Directory"),
0, NULL, 0, KEY_WRITE, NULL, &hkey, &dwDisposition);