?
Solved

How to read the value of a register key ?

Posted on 2011-03-24
11
Medium Priority
?
510 Views
Last Modified: 2012-05-11
I have the defined a registry key with the following value:
HKEY_LOCAL_MACHINE\SOFTWARE\Systemtipp\2011\DirPath = "C:\Programfiler\Systemtipp2011".

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::InteropServices;
      using namespace System::Collections;
      using namespace System::Security::Permissions;
      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;

Open in new window

0
Comment
Question by:toyboy61
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:r3nder
ID: 35212780
Try something like this
CString ReadProxyServer()
{
	CString cSvar = _T("");
	HKEY hKey;
	if (::RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\Currentversion\\Internet Settings"),
				0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
	{
		TCHAR szData[256];
		DWORD dwKeyDataType;
		DWORD dwDataBufSize = 256;
		if (::RegQueryValueEx(hKey, _T("ProxyServer"), NULL, &dwKeyDataType, // /"ProxyServer"
				(LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
		{
			switch ( dwKeyDataType )
			{
				case REG_SZ:
					cSvar = CString(szData);
					break;
			}
		}
		::RegCloseKey( hKey );
	}
	return cSvar;
}

Open in new window

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 total points
ID: 35213105
Do you mean it fails at: textBox14->Text = SysPtr->ToString;

If yes then I guess the SysPtr is null, which means that the GetValue called returned null - nothing found in the registry?
0
 
LVL 6

Expert Comment

by:r3nder
ID: 35214832
Yes - did you verify as well
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:toyboy61
ID: 35217411
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 ?



0
 

Author Comment

by:toyboy61
ID: 35217521
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::CString' : 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 ??

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: }

Open in new window

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 total points
ID: 35217550
toyboy61 - that is classical C++, not managed C++ which is why it gives loads of errors.

CHECK the return value is null - do you nkow how to use breakpoints?
0
 
LVL 6

Expert Comment

by:r3nder
ID: 35217654
I cant tell you about 2003 - I use 8
0
 

Author Comment

by:toyboy61
ID: 35218208
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::LocalMachine->GetValue(S"\\SOFTWARE\\Systemtipp\\2011\\DirPath"));" 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_MACHINE\SOFTWARE\Systemtipp\2011\DirPath ==> C:\Programfiler\Systemtipp2011.

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

0
 
LVL 6

Accepted Solution

by:
r3nder earned 1600 total points
ID: 35218273
That API haqsnt changed in 15 years
reg open key
reg read value
maybe you can get this one
// registry_read.cpp
// compile with: /clr
using namespace System;
using namespace Microsoft::Win32;

int main( )
{
   array<String^>^ key = Registry::CurrentUser->GetSubKeyNames( );

   Console::WriteLine("Subkeys within CurrentUser root key:");
   for (int i=0; i<key->Length; i++)
   {
      Console::WriteLine("   {0}", key[i]);
   }

   Console::WriteLine("Opening subkey 'Identities'...");
   RegistryKey^ rk = nullptr;
   rk = Registry::CurrentUser->OpenSubKey("Identities");
   if (rk==nullptr)
   {
      Console::WriteLine("Registry key not found - aborting");
      return -1;
   }

   Console::WriteLine("Key/value pairs within 'Identities' key:");
   array<String^>^ name = rk->GetValueNames( );
   for (int i=0; i<name->Length; i++)
   {
      String^ value = rk->GetValue(name[i])->ToString();
      Console::WriteLine("   {0} = {1}", name[i], value);
   }

   return 0;
}

Open in new window

0
 

Assisted Solution

by:toyboy61
toyboy61 earned 0 total points
ID: 35218520
r3nder:  "array<String^>^" : the keyword "array" in this context is not supported in Visual C++ 2003. (I have tried). :-).  Neither is the use of caret. I'm stuck with the old expression type "*".

The attached code snippet works(!).
RegistryKey* rk = NULL;
  rk = Registry::LocalMachine->OpenSubKey("SOFTWARE\\Systemtipp\\2011");
  
  if (rk == NULL) textBox14->Text = "Registry-key was not found..";
  else
  {
     String* value = rk->GetValue("DirPath")->ToString();
     textBox13->Text = value; 
  }

Open in new window

0
 

Author Closing Comment

by:toyboy61
ID: 35275374
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..
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question