Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I try to read a value from registry, I try this code
Note
: based on Windows7
: using MSVC++2010 as tool



C++
#include <windows.h>
#include <malloc.h>
#include <stdio.h>

#define TOTALBYTES    8192


void main()
{
    DWORD BufferSize = TOTALBYTES;
    DWORD cbData;
    DWORD dwRet;
	HKEY result;

    PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );

	dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
							TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\"),
							0,
							KEY_ALL_ACCESS,
							&result
		);
    if( dwRet == ERROR_SUCCESS )
        printf("\n\RegOpenKeyEx result : %d, function successed\n", &result);
    else printf("\nRegOpenKeyEx failed (%d)\n", dwRet);
	
	dwRet = RegQueryValueEx( HKEY_LOCAL_MACHINE,
                             TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz"),
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData );
	if( dwRet == ERROR_SUCCESS )
        printf("\n\nRegQueryValueEx result(~MHz) %d, function successed\n", &cbData);
    else printf("\nRegQueryValueEx(~MHz) failed (%d)\n", dwRet);

	dwRet = RegQueryValueEx( HKEY_LOCAL_MACHINE,
                             TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData);
	if( dwRet == ERROR_SUCCESS )
        printf("\n\nRegQueryValueEx result %d, function successed\n", &cbData);
    else printf("\nRegQueryValueEx failed (%d)\n", dwRet);

	dwRet = RegCloseKey(result);
	if( dwRet == ERROR_SUCCESS )
        printf("\n\nRegCloseKey successed\n");
    else printf("\nRegCloseKey failed (%d)\n", dwRet);


	system("pause");
}


Why two paths in RegQueryValueEx() is not working, as it works on RegOpenKeyEx() and RegCloseKey()?
Posted
Updated 21-Dec-11 23:40pm
v2
Comments
CPallini 22-Dec-11 5:35am    
What is the error return value (dwRet)?
windyl3ig 22-Dec-11 5:52am    
no, not return value but it show,

nRegQueryValueEx(~MHz) failed (2)
nRegQueryValueEx failed (2)

That means, File not Found, why file is not found? because I used the same path with RegOpenKeyEx()
Jochen Arndt 22-Dec-11 5:58am    
Why you do not pass the opened key handle: RegQueryValueEx(result, "~MHz", ...)?
windyl3ig 22-Dec-11 6:22am    
That 's not work too, 2 still be returned.

1 solution

Because, as already noted by Jochen Arndt, RegQueryValueEx does NOT work that way: you misunderstood the function documentation. Change, for instance
windyl3ig wrote:
VB
dwRet = RegQueryValueEx( HKEY_LOCAL_MACHINE,
                             TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz"),
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData );


to
C++
DWORD dwMHz;
cbData = sizeof(dwMHz);
dwRet = RegQueryValueEx( result,
                         TEXT("~MHz"),
                         NULL,
                         NULL,
                         (LPBYTE) &dwMHz,
                         &cbData );
 
Share this answer
 
v2
Comments
windyl3ig 22-Dec-11 6:41am    
Ah,that's my misunderstand. Thank you for every comment and guildline.
CPallini 22-Dec-11 6:54am    
You are welcome.
Monjurul Habib 22-Dec-11 14:30pm    
5!
CPallini 22-Dec-11 15:20pm    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900