C++ Registry Add nothing happen - c++

I have admin rights on PC, windows and try add registry DWORD key with CMD and ShellExecute.
All others commands via this way is working, but not Reg Add.
ShellExecute(0, "open", "cmd.exe", "/C reg add \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\SpecialAccounts\\Userlist\" /v username /t REG_DWORD /d 0 /f", 0, SW_HIDE);
When i try nothing happens. Why?
I have tried running the above command directly in the cmd with success, but when running my application the key is not added.
Thx.
Ok guys i try use another way using this code:
HKEY hKey;
LPCSTR sKeyPath;
int iResult;
sKeyPath = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\SpecialAccounts\\Userlist";
iResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyPath, NULL, KEY_ALL_ACCESS, &hKey);
DWORD value = 0x00000000;
iResult = RegSetValueEx(hKey, "username", NULL, REG_DWORD, (const BYTE*)&value, sizeof(value));
RegCloseKey(hKey);
Don't work too :(
I try this instead, but fail
HKEY hKey;
_TCHAR sKeyPath[] = _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\SpecialAccounts\\Userlist");
RegCreateKeyEx(HKEY_LOCAL_MACHINE, sKeyPath, 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
RegSetValueEx(hKey, _T("username"), 0, REG_DWORD, (BYTE*)_T("000000"), sizeof(_T("000000")));

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?

C++ Registry Edit

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);
}
}

Can not disable Device Manager using a C++ program

I want to disable Device Manager from my control panel editing registry values. I can do it in C#, but I want to do it in C++ without using any .NET framework. I have succedded to change my processor name in C++. But I am facing a problem when I want to disable the task manager. Here is my code.
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
0,
KEY_SET_VALUE,
&hKey);
RegSetValueEx(hKey, REGNAME_TO_WRITE, 0, REG_SZ,
(const unsigned char *)"ProcessorNameString",
strlen("ProcessorNameString"));
//RegCloseKey(hKey);
// The problem begins here
RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"Software\\Policies\\Microsoft\MMC\\{74246bfc-4c96-11d0-abef-0020af6b0b7a}\\",
0,
KEY_SET_VALUE,
&hKey );
RegSetValueEx( hKey,"Restrict_Run",0,REG_SZ,
(const unsigned char *)"1",
strlen("1") );
RegCloseKey(hKey);
return 0;
}
You should disable WOW64 registry redirection, or else your program may make changes to WOW6432Node instead of HKEY_LOCAL_MACHINE.
See Disabling registry redirection for a registry key on an x64 platform
Viola, I got the solution. The solution would be like this:
DWORD dwVal = 1;
HKEY hKey = HKEY_CURRENT_USER;
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Policies\\Microsoft\\MMC\\{74246bfc-4c96-11d0-abef-0020af6b0b7a}\\", 0, KEY_ALL_ACCESS, &hKey);
RegSetValueEx (hKey, "Restrict_Run", 0, REG_DWORD, (LPBYTE)&dwVal, sizeof(DWORD));
RegCloseKey(hKey);

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);