toyboy61
asked on
How to read the value of a register key ?
I have the defined a registry key with the following value:
HKEY_LOCAL_MACHINE\SOFTWAR E\Systemti pp\2011\Di rPath = "C:\Programfiler\Systemtip p2011".
What I want is to read this value from my Visual C++ 2003 program using .NET-functionality, and to be able to use this information to open files on the given directory (with a given sub-directory called "systems" directly beneath \Systemtipp2011\.).
I have tried to use the RegistryKey with different methods, but all of them are giving me errors.
Anyone who have a code example to show me how to do this ?
In the attached code snippet I have tried to solve it, but without any luck. The code compiles OK, but when I try to run it the last line fails with the error : "Object reference not set to an object instance".
I'm using the following namespaces:
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::InteropSe rvices;
using namespace System::Collections;
using namespace System::Security::Permissi ons;
using namespace Microsoft::Win32;
So - what have I done wrong (or don't understood) here ? :-(
HKEY_LOCAL_MACHINE\SOFTWAR
What I want is to read this value from my Visual C++ 2003 program using .NET-functionality, and to be able to use this information to open files on the given directory (with a given sub-directory called "systems" directly beneath \Systemtipp2011\.).
I have tried to use the RegistryKey with different methods, but all of them are giving me errors.
Anyone who have a code example to show me how to do this ?
In the attached code snippet I have tried to solve it, but without any luck. The code compiles OK, but when I try to run it the last line fails with the error : "Object reference not set to an object instance".
I'm using the following namespaces:
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::InteropSe
using namespace System::Collections;
using namespace System::Security::Permissi
using namespace Microsoft::Win32;
So - what have I done wrong (or don't understood) here ? :-(
RegistryKey* SysPtr = dynamic_cast<RegistryKey*>(Registry::LocalMachine->GetValue(S"\\SOFTWARE\\Systemtipp\\2011\\DirPath"));
textBox14->Text = SysPtr->ToString;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes - did you verify as well
ASKER
r3nder: I looked up "Using CString" on msdn.microsoft.com, and it is only referenced for Visual Studio 2005, 2008 and 2010. Does that mean that it is not supported for Visual Studio 2003 ?
r3nder & AndyAinscow: I created the registry key (as mentioned in my first posting) using "regedit". So the key should be there, but it seems that my call returns null. But why ?
r3nder & AndyAinscow: I created the registry key (as mentioned in my first posting) using "regedit". So the key should be there, but it seems that my call returns null. But why ?
ASKER
r3nder: I tried to build the attached code snippet.
The following errors occured:
Form1.h(1418) : error C2146: syntax error : missing ';' before identifier 'ReadRegistryKey'
Form1.h(1418) : error C2501: 'Systemtipp2011::Form1::CS tring' : missing storage-class or type specifiers
Form1.h(1441) : warning C4183: 'ReadRegistryKey': missing return type; assumed to be a member function returning 'int'.
So what do I do now ??
The following errors occured:
Form1.h(1418) : error C2146: syntax error : missing ';' before identifier 'ReadRegistryKey'
Form1.h(1418) : error C2501: 'Systemtipp2011::Form1::CS
Form1.h(1441) : warning C4183: 'ReadRegistryKey': missing return type; assumed to be a member function returning 'int'.
So what do I do now ??
1418: CString ReadRegistryKey()
1419: {
1420: CString cSvar = _T("");
1421: HKEY hKey;
1422: if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Systemtipp\\2011\\DirPath"),
1423: 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
1424: {
1425: TCHAR szData[256];
1426: DWORD dwKeyDataType;
1427: DWORD dwDataBufSize = 256;
1428: if (::RegQueryValueEx(hKey, _T("DirPath"), NULL, &dwKeyDataType, // /"ProxyServer"
1429: (LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
1430: {
1431: switch ( dwKeyDataType )
1432: {
1433: case REG_SZ:
1434: cSvar = CString(szData);
1435: break;
1436: }
1437: }
1438: ::RegCloseKey( hKey );
1439: }
1440: return cSvar;
1441: }
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I cant tell you about 2003 - I use 8
ASKER
AndyAinscow: When I checked my original code I found that SysPtr has an undefined value after the statement given below was executed.
Does that mean that my statement "RegistryKey* SysPtr = dynamic_cast<RegistryKey*> (Registry: :LocalMach ine->GetVa lue(S"\\SO FTWARE\\Sy stemtipp\\ 2011\\DirP ath"));" is invalid, even though the compiler accepts it ? How can I access this value using .NET (Framework 1.1) ?
Using regedit I created the following value in the key:
MyComputer\HKEY_LOCAL_MACH INE\SOFTWA RE\Systemt ipp\2011\D irPath ==> C:\Programfiler\Systemtipp 2011.
r3nder: I said in my initial question that I want a solution using Visual Studio 2003 with .NET-functionality (which means "managed code" - based on the .NET 1.1 Framework..).
Does that mean that my statement "RegistryKey* SysPtr = dynamic_cast<RegistryKey*>
Using regedit I created the following value in the key:
MyComputer\HKEY_LOCAL_MACH
r3nder: I said in my initial question that I want a solution using Visual Studio 2003 with .NET-functionality (which means "managed code" - based on the .NET 1.1 Framework..).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did solve the problem myself after getting some example code from r3nder which I could use as a guidance to create my own code.
But also comments from AndyAinscow directed me in the correct direction..
But also comments from AndyAinscow directed me in the correct direction..
Open in new window