C++ and C# question

Posts 1–3 of 3 · Page 1 of 1
C++ and C# question
does c++ have all the registry functions c# does
Not in the core language, because the "Registry" is specific to Windows, and C++ isn't. But there are Windows functions to do it ofc.

https://www.google.com/search?q=C%2B%2B+Registry+access
c++ - How to read a value from the Windows registry - Stack Overflow

quote from s.o link above

Here is some pseudo-code to retrieve the following:

If a registry key exists
What the default value is for that registry key
What a string value is
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;
}

edited Oct 17 '12 at 15:01
Lou Franco
45.6k863114

answered Aug 30 '08 at 5:51
Brian R. Bondy
128k50353498



If for some inexplicable reason this never seems to find a key it may be a 32-bit/64-bit problem. See stackoverflow.com/q/15084380/482758 – Quail Sep 7 '13 at 7:47
.
...
Include the library dependency: Advapi32.lib
edit: Same registry functions can probably be found in some windows .dll.
example: RegQueryValueEx (used above)
https://www.google.com/search?q=RegQueryValueEx
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Header Winreg.h (include Windows.h)
Library Advapi32.lib
DLL Advapi32.dll
..so, yes? : ) I think.
Just to add on.

In short yes, if you are using the windows api and header file all the reg functions are included. As a general rule you can do anything with c++.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us