Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get the DWORD registry value on local machine?

Posted on 2009-07-09
6
Medium Priority
?
640 Views
Last Modified: 2012-05-07
Hi, I want to retrieve the DWORD registy value in DWORD type variable. Could anyone tell me how to query directly DWORD value and stored it in DWORD type variable. I know how to retrieve string registry key. See attached code.

Thanks.
HKEY lHKeyhandle;
HKEY lhkey;
LONG lRetVal;
char* mykey;
lRetVal = RegConnectRegistry(NULL, HKEY_LOCAL_MACHINE, &lHKeyhandle);
if(lRetVal == ERROR_SUCCESS) {		
	lRetVal = RegOpenKeyEx(lHKeyhandle, "SOFTWARE\\Microsoft\\Software\\MyApp", 0, KEY_QUERY_VALUE, &lhkey);
	if(lRetVal == ERROR_SUCCESS) {  	
		LPBYTE lpData = new BYTE[_MAX_FNAME + 1];
		ZeroMemory(lpData, _MAX_FNAME + 1);
		DWORD lpcbData = _MAX_FNAME;			
                                          lRetVal = RegQueryValueExA(lhkey, TEXT("MyKey"), NULL, NULL, lpData, &lpcbData);
                                          mykey = new char[_MAX_FNAME];
		if(lRetVal == ERROR_SUCCESS)
		    sprintf(mykey, "%s", lpData);		
		RegCloseKey(lhkey);
	}
	RegCloseKey(lHKeyhandle);
}

Open in new window

0
Comment
Question by:deshaw
[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
  • 3
  • 2
6 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 600 total points
ID: 24816417
http://msdn.microsoft.com/en-us/library/ms724868(VS.85).aspx

LONG WINAPI RegGetValue(
  __in         HKEY hkey,
  __in_opt     LPCTSTR lpSubKey,
  __in_opt     LPCTSTR lpValue,
  __in_opt     DWORD dwFlags,
  __out_opt    LPDWORD pdwType,
  __out_opt    PVOID pvData, //<-------------  &dwData
  __inout_opt  LPDWORD pcbData
);

Pass pvData as the address of a DWORD variable
0
 
LVL 86

Accepted Solution

by:
jkr earned 1400 total points
ID: 24817087
Or, in your code:
HKEY lHKeyhandle;
HKEY lhkey;
LONG lRetVal;
char* mykey;
lRetVal = RegConnectRegistry(NULL, HKEY_LOCAL_MACHINE, &lHKeyhandle);
if(lRetVal == ERROR_SUCCESS) {          
        lRetVal = RegOpenKeyEx(lHKeyhandle, "SOFTWARE\\Microsoft\\Software\\MyApp", 0, KEY_QUERY_VALUE, &lhkey);
        if(lRetVal == ERROR_SUCCESS) {          
                DWORD dwValue;
                
                DWORD lpcbData = sizeof(DWORD);                    
                                          lRetVal = RegQueryValueExA(lhkey, TEXT("MyKey"), NULL, NULL, (LPBYTE) &dwValue, &lpcbData);
                                         
                if(lRetVal == ERROR_SUCCESS)
                    sprintf(mykey, "%d", dwValue);               
                RegCloseKey(lhkey);
        }
        RegCloseKey(lHKeyhandle);
}

Open in new window

0
 
LVL 1

Author Comment

by:deshaw
ID: 24817155
Thanks both of you. The posts were helpful. My solution is below and working fine. Let me know if you think there is any amiss.
HKEY lhkey;
LONG lRetVal;
DWORD dwType, dwSize;
//set default value
DWORD mykey= 0;

try {
     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyApp", 0, KEY_QUERY_VALUE, &lhkey) == ERROR_SUCCESS) {
    dwType = REG_DWORD;
    dwSize = sizeof(DWORD);
    RegQueryValueEx(lhkey, TEXT("myRegKey"), NULL, &dwType, (PBYTE)&mykey, &dwSize);
    RegCloseKey(lhkey);
    }
} catch(...) {
//ignore and take default value
}
Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 1400 total points
ID: 24817246
You can omit try/catch here, since no C++ exceptions are going to be thrown. SEH might be more appropriate, e.g.
__try {
     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyApp", 0, KEY_QUERY_VALUE, &lhkey) == ERROR_SUCCESS) {
    dwType = REG_DWORD;
    dwSize = sizeof(DWORD);
    RegQueryValueEx(lhkey, TEXT("myRegKey"), NULL, &dwType, (PBYTE)&mykey, &dwSize);
    RegCloseKey(lhkey);
    }
} __except(1) {
//ignore and take default value
} 

Open in new window

0
 
LVL 1

Author Comment

by:deshaw
ID: 24821002
ohh..great..thanks jkr...
0
 
LVL 1

Author Closing Comment

by:deshaw
ID: 31601724
Thanks...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

715 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