PDA

You are currently viewing a search engine-friendly (archive) version of this page.

View Full Version : C++ Query Registry


afterthought
November 14, 2001, 02:59 pm
Okay, I am officially stuck. Lets see if anyone can help me with this.

I am triing to querry the registry for the processor speed. Unfortunatley, I cannot get it
to work.

I can not get 'Value_data' to print the value I want. It works if I redirect 'Value' to a string like 'Identifier' or 'VendorIdentifier'. But when I direct it to '~MHz' it returns a bunch of silly numbers. I tried changing the 'printf(" . . . %d MHz", Value_data) to '%c', '%X', '%x', and '%s' without any success.

Here is the code:

-----------------------------------------------
#include <windows.h>
#include <winreg.h>
#include <stdlib.h>
#include <stdio.h>

void main (void)
{
HKEY hKeyRoot,hKeyNew;
LPCTSTR subkey;
LPTSTR Value;
DWORD retcode;
DWORD Value_type;
BYTE *Value_data;
DWORD Value_size;

hKeyRoot = HKEY_LOCAL_MACHINE;
subkey = "Hardware\\Description\\System\\CentralProcessor\\0";
Value = "~MHz";
Value_data = new BYTE[80];
Value_type = REG_SZ;

retcode = RegOpenKeyEx
(
hKeyRoot,
subkey,
0,
KEY_QUERY_VALUE,
&hKeyNew
);

if (retcode != ERROR_SUCCESS)
printf("Unable to open key\n");

retcode = RegQueryValueEx
(
hKeyNew,
Value,
NULL,
&Value_type,
Value_data,
&Value_size
);

if (retcode != ERROR_SUCCESS)
printf("Unable to read key\n");
else
printf("\n\nValue_data is: \n%d MHz \n\n", Value_data); //This is what I want.

RegCloseKey(hKeyRoot);

system("PAUSE");
return 0;
}
------------------------------------------------
I tried looking at:
<a href="http://www.msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/regapi_3wzc.asp?fram=true" target="_blank">http://www.msdn.microsoft.com/library/default.asp?url=/library/en-us/sys info/regapi_3wzc.asp?fram=true</a>

and

<a href="http://www.codeguru.com/system/HardwareInfo.shtml" target="_blank">http://www.codeguru.com/system/HardwareInfo.shtml</a>

but I was still unable to solve my problem.

[ November 14, 2001: Message edited by: afterthought ]</p>

afterthought
November 21, 2001, 10:25 am
Okay, I think I figured this out. I needed to play with my pointers (everybody does it).

It should look like this:

HKEY hKeyRoot,hKeyNew;
LPCTSTR lpSubKey;
LPTSTR Value;
DWORD retcode;
DWORD Value_type;
DWORD Value_data;
DWORD Value_size;

hKeyRoot = HKEY_LOCAL_MACHINE;
lpSubKey = "Hardware\\Description\\System\\CentralProcessor\\0";
Value = "~MHz";

retcode = RegOpenKeyEx
(
hKeyRoot,
lpSubKey,
0,
KEY_QUERY_VALUE,
&hKeyNew
);
if (retcode != ERROR_SUCCESS)
printf("Error opening key");

retcode = RegQueryValueEx
(
hKeyNew, // HKEY (hkey) handle of key to query
Value, // LPSTR (lpValueName) address of name of value to query
NULL, // LPDWORD (lpReserved) reserved
&Value_type, // LPDWORD (lpType) address of buffer for value type
(BYTE *) &Value_data,// LPBYTE (lpData) address of data buffer
&Value_size // LPDWORD (lpcbData) address of data buffer size
);
if (retcode != ERROR_SUCCESS)
printf("Error reading key,");
else
printf("%d ,", Value_data);

RegCloseKey(hKeyRoot);

Tonker
November 21, 2001, 12:30 pm
Glad to hear you were able to figure it out! I haven't played around with ANY windows programming, yet... I suppose it's something I should look into. :D Got any hints for good sources for learning win32 stuff?

[ November 21, 2001: Message edited by: Tonker ]</p>

afterthought
January 15, 2002, 01:45 pm
No hints from me! I'm still struggling myself!

I have learned that when working with win32 (Windows products) Microsoft's MSDN is about as usefull as a pile of dead rats in a tampon factory.

Most of the examples don't work or are missing headers, and it is not very easy reading.

Please excuse my colorfull metaphore.