What code add to this function to work good? (ERROR_SUCCESS)
I have code, that check value in registry.
In function RegQueryValueEx is bug.
When oldValue is few letters longer than newValue, function shows ERROR_MORE_DATA, but I want want ERROR_SUCCESS
What code add to this function to do this?
void function(string newValue, string key, string name)
{
// string key - key in registry, ie Myapp\\Options
// string name - name in registry
// string newValue - data in REG_SZ
string oldValue;
DWORD keytype = REG_SZ;
HKEY keyHandle;
DWORD size = sizeof(string);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(),0L,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
{
LONG isgood = RegQueryValueEx(keyHandle, name.c_str(), 0, &keytype, (LPBYTE)&oldValue, &size);
if(isgood == ERROR_MORE_DATA)
{
cout << "Error more data\n";
}
if(isgood == ERROR_SUCCESS)
{
cout << "Old data is " << oldValue.c_str() << endl;
cout << "New data is " << newValue.c_str() << endl;
if(strcmp(newValue.c_str(), oldValue.c_str()) != 0) // compare 2 strings, if
{
cout << "String 1 and string 2 are different";
}
else
{
cout << "String 1 and string 2 are the same";
}
}
if(isgood == ERROR_FILE_NOT_FOUND)
{
cout << "Name in registry not found!";
}
}
}
ERROR_MORE_DATA means that you need to pass in a larger string buffer. The typical pattern you'll need to use is to call once to get the size, then allocate a properly-sized buffer, then call again. Or, alternatively, you can guess at a size, pass in that-sized buffer, and increase size if you get ERROR_MORE_DATA back.
BTW, you are also computing size incorrectly. And you're not closing the registry key. And you're not prepared to support being compiled under unicode or non-unicode modes.
Here's some revised code which addresses these issues.
#include <string>
#include <vector>
#include <iostream>
#include <windows.h>
using namespace std;
namespace std
{
#ifdef _UNICODE
#define tcout wcout
#define tcin wcin
typedef wstring tstring;
#else
#define tcout cout
#define tcin cin
typedef string tstring;
#endif
};
void function(tstring newValue, tstring key, tstring name)
{
// string key - key in registry, ie Myapp\\Options
// string name - name in registry
// string newValue - data in REG_SZ
HKEY keyHandle;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(),0L,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
{
DWORD size = 500; // initial size
vector<TCHAR> buf(size);
tstring oldValue;
DWORD keytype = REG_SZ;
LONG isgood = RegQueryValueEx(keyHandle, name.c_str(), 0, &keytype, (LPBYTE) &buf[0], &size);
if(isgood == ERROR_SUCCESS)
{
oldValue.assign (&buf[0], size);
}
else if(isgood == ERROR_MORE_DATA)
{
buf.reserve (size); // expand to however large we need
isgood = RegQueryValueEx(keyHandle, name.c_str(), 0, &keytype, (LPBYTE)&buf[0], &size);
if(isgood == ERROR_SUCCESS)
oldValue.assign (&buf[0], size);
}
RegCloseKey (keyHandle); // remember to close this!
if(isgood == ERROR_SUCCESS)
{
tcout << _T("Old data is ") << oldValue << endl;
tcout << _T("New data is ") << newValue << endl;
if(newValue.compare(oldValue) != 0) // compare 2 strings, if
{
tcout << _T("String 1 and string 2 are different");
}
else
{
tcout << _T("String 1 and string 2 are the same");
}
}
if(isgood == ERROR_FILE_NOT_FOUND)
{
tcout << _T("Name in registry not found!");
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
tstring val;
function (val, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), _T("CommonFilesDir"));
return 0;
}
ERROR_MORE_DATA means the buffer you supplied to hold the data is not big enough.
Your problems are multiple:
When you say sizeof(string) you're getting the size of the string data type, not the length of the string. You should call string::size() to get the length of the string.
You can't just cast a string to an LPBYTE. That is going to FAIL miserably. The Registry APIs are not designed to work with strings, they are designed to work with char* and WCHAR* types. You need to declare a local character array (e.g. char *foo = new char[256]) and then pass that. If you get ERROR_MORE_DATA, declare a bigger one.
Related
The below code can correctly read Registry values from various different keys, however whenever I try to read a value from a key under Winlogon it will either come up as "not found" or it will return a completely wrong value. The code is ran as admin, and compiled with Visual Studio 2017.
HKEY registryHandle = NULL;
int registryResult = NULL;
DWORD dataType;
TCHAR dataBuffer[1024] = {};
DWORD bufferSize = sizeof(dataBuffer);
registryResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", 0, KEY_QUERY_VALUE, ®istryHandle);
if (registryResult != ERROR_SUCCESS) {
std::cout << "Error: " << registryResult << std::endl;
return false;
}
registryResult = RegQueryValueEx(registryHandle, L"LastUsedUsername", NULL, NULL, (LPBYTE)dataBuffer, &bufferSize);
if (registryResult != ERROR_SUCCESS) {
std::cout << "Error2: " << registryResult << std::endl;
return false;
}
std::cout << "Data Size: " << bufferSize << std::endl;
for (int i = 0; i < 256; i++) {
if (dataBuffer[i] == NULL) { break; }
std::cout << (char)dataBuffer[i];
}
std::cin.get();
RegCloseKey(registryHandle);
Registry value that I'm trying to read:
Below refers to Remy's suggested solution.
RegQueryValueEx Returns a buffer size of 4 with an output of 18754 17236 0 52428
You are clearly calling the Unicode version of the Registry functions, so you should be using WCHAR instead of TCHAR for your data buffer.
And you should not be truncating the characters to char at all. Use std::wcout instead of std::cout for printing out Unicode strings. And use the returned bufferSize to know how many WCHARs were actually output. Your printing loop is ignoring the bufferSize completely, so it is possible that you are actually printing out random garbage that RegQueryValueEx() did not actually intend for you to use (hence why lpcbData parameter is an in/out parameter, so you know how many bytes are actually valid).
You are also leaking the opened HKEY handle if RegQueryValueEx() fails.
Try something more like this instead:
HKEY registryHandle;
int registryResult;
registryResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", 0, KEY_QUERY_VALUE, ®istryHandle);
if (registryResult != ERROR_SUCCESS) {
std::cout << "Error: " << registryResult << std::endl;
return false;
}
WCHAR dataBuffer[1024];
DWORD bufferSize = sizeof(dataBuffer);
// TODO: consider using RegGetValueW() instead, which is safer
// when it comes to reading string values from the Registry...
registryResult = RegQueryValueExW(registryHandle, L"LastUsedUsername", NULL, NULL, (LPBYTE)dataBuffer, &bufferSize);
RegCloseKey(registryHandle);
if (registryResult != ERROR_SUCCESS) {
std::cout << "Error2: " << registryResult << std::endl;
return false;
}
DWORD len = bufferSize / sizeof(WCHAR);
if ((len > 0) && (dataBuffer[len-1] == L'\0')) {
--len;
}
std::cout << "Data Byte Size: " << bufferSize << std::endl;
std::cout << "Data Character Length: " << len << std::endl;
std::wcout.write(dataBuffer, len);
std::cin.get();
return true;
That being said, on my machine, there is no LastUsedUsername value in the Winlogon key you are accessing, so getting a "not found" error is a very likely possibility. But you definately need to handle
I would like to check the UAC configuration settings of windows. And thus recover the parameters of the UAC in the registry keys.
I used the windows SHGetValue function but the status always returns me 2 without any information.
I use C++11, MinGW and windows.
My code is :
DWORD dwStatus;
LPCSTR pszSubKey= "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
LPCSTR pszValue="";
DWORD pdwType=REG_SZ;
PVOID pvData[63];
DWORD pcbData;
pcbData=sizeof(pvData);
dwStatus=SHGetValueA(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &pdwType, pvData, &pcbData);
//Here dwStatus = 2
// pvData = 0x11fd0b2
// pcbData = 504
What specific key you are trying to read? I am not an expert on win32 API so I don't know whether there is a way to read a set of keys at once (Edit: I think there areRegEnumValue/RegEnumValueA functions for this purpose). Here is an example that shows how you can read "EnableLUA" or any other key from that path:
#include <windows.h>
#include <iostream>
#include <shlwapi.h>
bool ReadUACRegistryKey(char* key, DWORD &keyValue)
{
LPCTSTR pszSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
LPCTSTR pszValue = key;
// don't care
DWORD dwType = 0;
DWORD dwValue = 0;
//
DWORD dwValueSize = sizeof(dwValue);
int retval = SHGetValue( HKEY_LOCAL_MACHINE, pszSubKey, key, &dwType, &dwValue, &dwValueSize);
if ( retval != ERROR_SUCCESS)
{
return false;
}
keyValue = dwValue;
return true;
}
int main()
{
DWORD keyValue;
char* key = "EnableLUA"; // "EnableSecureUIAPaths" etc..;
if (ReadUACRegistryKey(key, keyValue))
{
std::cout << "Successfully readed key " << key << ", value:" << keyValue << std::endl;
}
else
{
std::cout << "Unable to read value of key " << key << std::endl;
}
return 0;
}
Also keep in mind that value of read key value is stored in value parameter, not in the return value of the function.
Edit: Answer of the the op's comment "I want use FilterAdministratorToken but is disable by default how give it back enable .?". Keep in mind that your process need to have admin rights to perform these operation.
#include <windows.h>
#include <iostream>
#include <shlwapi.h>
bool ReadUACRegistryKey(char* key, DWORD &keyValue)
{
LPCTSTR pszSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
LPCTSTR pszValue = key;
// don't care
DWORD dwType = 0;
DWORD dwValue = 0;
//
DWORD dwValueSize = sizeof(dwValue);
int retval = SHGetValue( HKEY_LOCAL_MACHINE, pszSubKey, key, &dwType, &dwValue, &dwValueSize);
if ( retval != ERROR_SUCCESS)
{
return false;
}
keyValue = dwValue;
return true;
}
bool EnableFilterAdministratorToken()
{
// first check if its already enabled or not
DWORD val;
if (ReadUACRegistryKey("FilterAdministratorToken", val))
{
if (val == 1)
{
std::cout << "FilterAdministratorToken is already enabled" << std::endl;
return true;
}
}
else
{
std::cout << "Unable to read key" << std::endl;
return false;
}
// its not enabled, we need to enable it manually
// obtain a handle to reg key
HKEY hKey;
int retval = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_SET_VALUE, &hKey);
if (retval != ERROR_SUCCESS)
{
// we are unable to obtain a handle to reg key
std::cout << "Unable to obtain handle to reg key" << std::endl;
return false;
}
DWORD enabledValue = 1;
retval = RegSetValueExA(hKey, "FilterAdministratorToken", 0, REG_DWORD, (BYTE*) &enabledValue, sizeof(DWORD));
if (retval != ERROR_SUCCESS)
{
// some error occured
std::cout << "Some error occured during setting the key value" << std::endl;
RegCloseKey(hKey);
return false;
}
std::cout << "Successfully changed key value" << std::endl;
RegCloseKey(hKey);
return true;
}
int main()
{
if (EnableFilterAdministratorToken())
{
std::cout << "OK" << std::endl;
}
else
{
std::cout << "FAIL" << std::endl;
}
return 0;
}
Basically, I want to iterate through the entire Windows registry HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG and delete every key that has a title or data that contains a string/char/any type of data. Issue is, I have no idea how I would actually approach this.
I've already seen snippets of code looking for keys with a name such as:
subkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
{
dwType = REG_SZ;
if( RegQueryValueEx(hKey,"AVG_TRAY",0,&dwType,(BYTE*)buffer,&dwBufferSize) == ERROR_SUCCESS )
{
cout << "key value is'" << buffer << "'\n";
}
else
cout << "can not query for key value\n";
}
or something like
int GetPaths()
{
char buffer[255] = {0};
DWORD dwBufferSize = sizeof(buffer);
const char* subkey;
string values[100]; // Max 100 values.
string Paths[100]; // Max 100 paths.
DWORD dwType = 0;
HKEY hKey = 0;
int i=0;
int j=0;
subkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
{
dwType = REG_SZ;
/** Here a loop that would call a function
* that would retrieve the name of the first
* value into values[i] (i would be 0 initially)
* and then increment 1 to i and loop again.
* The value would be the next one and its
* name would be save in values[i] (i would be
* 1 this time). It would keep until all the
* values in hKey would have been read.
*/
while( i =< hKey.numberofvalues )
{
GetValueName(from HKey, valuenumber i, into buffer);
values[i] = buffer;
i++;
}
/** Now a loop to get the data of thoose values
* (values[j]) into buffer and then into Paths[j].
*/
while( j =< i )
{
if(RegQueryValueEx(hKey,values[j],0,&dwType,(BYTE*)buffer,&dwBufferSize) == ERROR_SUCCESS)
{
Paths[j] = buffer;
j++;
}
else
cout << "Could not query value from " << hKey << endl;
}
}
else
cout << "Could not open key " << subkey << endl;
cout << "Got paths from registry successfully." << endl;
RegCloseKey(hKey);
}
but they don't answer my question.
If anyone could help me out, it would be appreciated.
Thanks.
I have the following code:
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main(int argc, wchar_t*argv[])
{
std::locale::global(std::locale("spanish"));
/*Declaración de variables*/
HKEY hKey = HKEY_CURRENT_USER;
LPCTSTR lpSubKey = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU");
DWORD ulOptions = 0;
REGSAM samDesired = KEY_READ | KEY_WRITE;
HKEY phkResult;
DWORD dwIndex = 0;
TCHAR lpValueName[16383];
DWORD lpcchValueName = 16383;
LPTSTR lpData="";
long OpenK = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &phkResult);
if (OpenK == ERROR_SUCCESS)
{
long R = RegEnumValue(phkResult, dwIndex, lpValueName, &lpcchValueName, NULL, NULL,(LPBYTE)lpData, NULL);
if (R == ERROR_SUCCESS)
{
cout << "The value and data is: \n" << lpValueName << ": " << lpData << endl;
//printf(TEXT("(%d) %s\n"), lpValueName);
}
else
cout << "Error: " << R << endl;
}
else if (OpenK == ERROR_FILE_NOT_FOUND)
{
cout << "La sub-clave RunMRU no existe." << endl;
}
else if (OpenK == ERROR_ACCESS_DENIED)
{
cout << "Acceso denegado al abrir la sub-clave RunMRU." << endl;
}
else
{
cout << "Error al abrir la clave de registro. Código: " << OpenK << endl;
}
system("Pause");
}
I am trying to show both, the Value name, and its Data using the RegEnumValue in the first if (The value and data is:) but I can only show the Value name.
Is there any way to do that? I'm trying to figure out how to use the lpData, but I can't because I only receive error 87 (Incorrect parameters) or nothing (If I set NULL instead).
You're not providing a suitable buffer for RegEnumValue() to store the data.
LPTSTR lpData="";
This is just a string literal, of at most 2 bytes in size, and is almost certainly not writable anyway. You need to allocate an area of memory and pass that to RegEnumValue() to read the data back for each value.
Your first step should be to use RegQueryInfoKey() to find out how big the largest data value is. I showed you how to use this function in a previous answer to query the size of the largest value name - the process is the same. See the docs for RegQueryInfoKey() to find out which parameter provides the data size.
Once you know how big your largest item of data is, allocate a buffer for it:
void* pData = malloc(dwLargestValueSize);
// remember this buffer needs to be freed at the end with free()
You then pass that buffer, plus a value indicating its size, to RegEnumValue().
Something else you need to be aware of is that registry values can be different types - REG_DWORD, REG_SZ, etc, and the data you get back from RegEnumValue() is the raw data. RegEnumValue() can also return a value indicating the type of data and if you're to properly interpret it, you absolutely need to check this as well.
Changes to your code to get a string value into lpData:
#define MAX_DATA_LENGTH 16383
char* lpData = new char[MAX_DATA_LENGTH];
DWORD lpDataLength = MAX_DATA_LENGTH;
RegEnumValue(phkResult, dwIndex, lpValueName, &lpcchValueName, NULL, NULL, (unsigned char*)lpData, &lpDataLength);
I have trouble making this code I found work for me.
Now: It adds 2 DWORD values the the Interface folder (registry key) in registry.
Desired: I would want it to add those 2 DWORD values to ALL the subkeys (subfolders) of the Interface registry key (folder).
I have got this pseudo code:
Open the parent key with RegOpenKey or RegOpenKeyEx
Enumerate all of the child keys of the parent using RegEnumKey or RegEnumKeyEx in a loop
For each child key, set the desired value with RegSetValueEx
Close the parent key with RegCloseKey
I'll keep trying to get this sorted, but maybe someone can help?
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <iostream>
using std::cout;
using std::endl;
HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
{
HKEY hKey;
LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);
if(nError==ERROR_FILE_NOT_FOUND)
{
cout << "Creating registry key: " << strKey << endl;
nError = RegCreateKeyEx(hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL);
}
if(nError)
{
cout << "Error: " << nError << " Could not find or create " << strKey << endl;
}
return hKey;
}
void SetVal(HKEY hKey, LPCTSTR lpValue, DWORD data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
if(nError)
{
cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl;
}
}
DWORD GetVal(HKEY hKey, LPCTSTR lpValue)
{
DWORD data;
DWORD size = sizeof(data);
DWORD type = REG_DWORD;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
if(nError==ERROR_FILE_NOT_FOUND)
{
data = 0; // The value will be created and set to data next time SetVal() is called.
}
else if(nError)
{
cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;
}
return data;
}
int main()
{
static DWORD v1, v2;
HKEY hKey = OpenKey(HKEY_LOCAL_MACHINE,L"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces\\");
v1 = GetVal(hKey, L"Registry Value1");
v2 = GetVal(hKey, L"Registry Value2");
v1 += 5;
v2 += 2;
SetVal(hKey, L"Registry Value1", v1);
SetVal(hKey, L"Registry Value2", v2);
RegCloseKey(hKey);
return 0;
}
Here's a bare minimum example without any extras:
// open desired key whose subkeys shall be enumerated
HKEY hKey={0};
LPCTSTR path=TEXT("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces");
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_ENUMERATE_SUB_KEYS,&hKey) != ERROR_SUCCESS)
return; // failed to open
DWORD index=0; // enumeration index
TCHAR keyName[256]={0}; // buffer to store enumerated subkey name
DWORD keyLen=256; // buffer length / number of TCHARs copied to keyName
// enumerate subkey names of hKey, result stored in keyName, keyLen set to strlen(keyName)
while(RegEnumKeyEx(hKey,index++,keyName,&keyLen,0,0,0,0) == ERROR_SUCCESS) {
keyLen=256; // reset buffer length (RegEnumKeyEx changes this value)
// open the subkey and set the desired value(s)
HKEY hSubKey={0};
if(RegOpenKeyEx(hKey,keyName,0,KEY_SET_VALUE,&hSubKey) == ERROR_SUCCESS) {
// set desired value(s):
DWORD myValue = 0xCAFEBABE;
//RegSetValueEx(hSubKey,TEXT("MyValueName"),0,REG_DWORD,(LPBYTE)&myValue,sizeof(DWORD));
RegCloseKey(hSubKey); // close sub key
}
// else: failed to open subkey
}
// RegEnumKeyEx either returns ERROR_SUCCESS, ERROR_NO_MORE_ITEMS, or a system error code
RegCloseKey(hKey); // close key
Please note, this example does not evaluate error codes. It simply demonstrates the process of enumerating sub keys and setting a value. The RegOpenKeyEx access rights are set to the minimum required to perform this task (set them to whatever you wish to do with the opened keys). The while loop does not distinct from ERROR_NO_MORE_ITEMS (once there are no more subkeys to enumerate) or an actual error. RegSetValueEx is commented out for safety and its return value is ignored.