How to Read a Value from the Windows Registry

How to read a value from the Windows registry

Here is some pseudo-code to retrieve the following:

  1. If a registry key exists
  2. What the default value is for that registry key
  3. What a string value is
  4. What a DWORD value is

Example code:

Include the library dependency: Advapi32.lib

HKEY hKey;
LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Perl", 0, KEY_READ, &hKey);
bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);
std::wstring strValueOfBinDir;
std::wstring strKeyDefaultValue;
GetStringRegKey(hKey, L"BinDir", strValueOfBinDir, L"bad");
GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad");

LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue)
{
nValue = nDefaultValue;
DWORD dwBufferSize(sizeof(DWORD));
DWORD nResult(0);
LONG nError = ::RegQueryValueExW(hKey,
strValueName.c_str(),
0,
NULL,
reinterpret_cast<LPBYTE>(&nResult),
&dwBufferSize);
if (ERROR_SUCCESS == nError)
{
nValue = nResult;
}
return nError;
}


LONG GetBoolRegKey(HKEY hKey, const std::wstring &strValueName, bool &bValue, bool bDefaultValue)
{
DWORD nDefValue((bDefaultValue) ? 1 : 0);
DWORD nResult(nDefValue);
LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult, nDefValue);
if (ERROR_SUCCESS == nError)
{
bValue = (nResult != 0) ? true : false;
}
return nError;
}


LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue)
{
strValue = strDefaultValue;
WCHAR szBuffer[512];
DWORD dwBufferSize = sizeof(szBuffer);
ULONG nError;
nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);
if (ERROR_SUCCESS == nError)
{
strValue = szBuffer;
}
return nError;
}

How to read integer value from Windows registry using Java?

I have found the solution:

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public class ReadRegistry
{
public static final String readRegistry(String location, String key){
try {
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query " +
'"'+ location + "\" /v " + key);

StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();

// Parse out the value
// String[] parsed = reader.getResult().split("\\s+");

String s1[];
try{
s1=reader.getResult().split("REG_SZ|REG_DWORD");
}
catch(Exception e)
{
return " ";
}
//MK System.out.println(s1[1].trim());

return s1[1].trim();
} catch (Exception e) {
}

return null;
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw= new StringWriter();

public StreamReader(InputStream is) {
this.is = is;
}

public void run() {
try {
int c;
while ((c = is.read()) != -1)
//System.out.println(c);
sw.write(c);
} catch (IOException e) {
}
}

public String getResult() {
return sw.toString();
}
}
}

You have to use it like this:

Registry2.readRegistry("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\LanmanWorkstation\\Parameters", "EnableSecuritySignature");

The class was taken from here.

C++ Can't read registry value data

The value MachineGuid in the key SOFTWARE\Microsoft\Cryptography in my environment is a string longer than 4 characters, not a DWORD value.

You have to allocate enough resion to read the value. Otherwise, ERROR_MORE_DATA will be returned as documented.

#include <cstdio>
#include <cstdlib>
#include <Windows.h>
int main()
{
char val[128];
DWORD dataSize = sizeof(val);
if (ERROR_SUCCESS == RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGuid", RRF_RT_ANY, nullptr, &val, &dataSize))
{
printf("Value is %.*s\n", (int)dataSize, val);
}
else
{
printf("Read Error");
};
system("pause");
return 1;
}

How to read (Default) value from registry and put into a variable within a batch file?

From the cmd line:

for /f "tokens=3*" %a in ('reg query "HKLM\SOFTWARE\Wow6432Node\Notepad++"') do echo %a %b

if you want to do it in script, double the %'s

Getting REG_DWORD from windows registry as a wstring

if (type != REG_SZ && type != REG_DWORD) ...

You just have to treat REG_SZ and REG_DWORD differently.

Also add an extra +1 for the null terminator to be safe.

wstring ReadRegValue(HKEY root, wstring key, wstring name)
{
HKEY hKey;
if (RegOpenKeyEx(root, key.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
throw "Could not open registry key";
DWORD type;
DWORD cbData;
if (RegQueryValueEx(hKey,
name.c_str(), NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
throw "Could not read registry value";
}

std::wstring value;
if (type == REG_SZ)
{
value.resize(1 + (cbData / sizeof(wchar_t)), L'\0');
if (0 == RegQueryValueEx(hKey, name.c_str(), NULL, NULL,
reinterpret_cast<BYTE*>(value.data()), &cbData))
{ //okay
}
}
else if (type == REG_DWORD)
{
DWORD dword;
if (0 == RegQueryValueEx(hKey, name.c_str(), NULL, &type,
reinterpret_cast<BYTE*>(&dword), &cbData))
value = std::to_wstring(dword);
}
RegCloseKey(hKey);
return value;
}


Related Topics



Leave a reply



Submit