How to read a registry entry

This is a discussion on How to read a registry entry within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I am new to C++ and I am struggling to understand something you guys probably take for granted! I ...

  1. #1
    rintsp
    Guest

    How to read a registry entry

    Hi,

    I am new to C++ and I am struggling to understand something you guys probably take for granted!

    I need to write some code to take in a registry key....for example

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\Nls\CodePage\ACP

    and return the String value...in this case 1252.

    Now I am struggling with the GetStringValue function, could someone show me an example of how this can be done?

    Cheers

    Pete

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    *cough* MSDN you freak!! *cough*

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    //#define UNICODE //Uncomment for unicode build (Faster on NT/XP)
    //#define _UNICODE//Uncomment for unicode build (Not for Win95/98)
    #include <windows.h>
    #include <tchar.h>
    
    
    int WINAPI WinMain(HINSTANCE hInst,
    				   HINSTANCE,LPSTR,int){
    
    	const int cBuffSize = 255;//Size of buffer
    	TCHAR szBuff[cBuffSize] = {0},//Actual buffer
    		  *szPath = //Path of key
    		_T("SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage");
    	HKEY hKey = NULL;//Key used to hget value
    	DWORD dwSize = cBuffSize; //To tell buff size and recieve actual size
    		 
    
    	//First open a key to allow you to read the registry
    	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,//Main key to browse
    		            szPath,//sub key
    					0,
    					KEY_READ,//access rights - we want to read
    					&hKey)//Recieve the key we want to use
    					!= ERROR_SUCCESS){
    		MessageBox(HWND_DESKTOP,_T("Could not open key"),NULL,MB_OK);
    		return 1;
    	}
    
    	if(RegQueryValueEx(hKey,//From previous call
    					   _T("ACP"),//value we want to look at
    					   0,
    					   NULL,//not needed,we know its a string
    					   (UCHAR*)szBuff,//Put info here
    					   &dwSize)//How big is the buffer?
    					   !=	ERROR_SUCCESS){
    		MessageBox(HWND_DESKTOP,_T("Could not get value"),NULL,MB_OK);
    		return 1;
    	}
    
    	MessageBox(HWND_DESKTOP,szBuff,_T("Result"),MB_OK);
    
    	RegCloseKey(hKey);//Dont forget to cleanup!!!!
    		
    	return 0;
    }
    >>*cough* MSDN you freak!! *cough*

    Cute!
    Last edited by Fordy; 09-07-2002 at 04:15 AM.

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >>*cough* MSDN you freak!! *cough*

    Cute!
    I'm just a regular good Samaratin

  5. #5
    rintsp
    Guest
    Rob,

    Thanks this is a start! I have moved the code into VC++ and hit compile, but it is complaining about *szPath.

    Message I am getting is :

    E:\lib\Cpp1.cpp(13) : error C2440: 'initializing' : cannot convert from 'char [46]' to 'unsigned short *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    The line is :

    TCHAR szBuff[cBuffSize] = {0},//Actual buffer
    *szPath = _T("SYSTEM\\CurrentControlSet\\Control\\Nls\\CodeP age");

    Am I doing something wrong here?

    Pete

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Err....Sorry I may not have made the first part of the code clear.....

    There are 2 defines.......for Win95 & win2000/XP leave them both commented....If you want a UNICODE build for windows NT/2000/XP then uncomment them both.......

    Sorry, I shouldt have bothered with the unicode stuff at all......got a little carried away in what I was doing......apologies

  7. #7
    rintsp
    Guest
    Sorry Rob, I must be doing something wrong here....

    I copy the code into a file called TEST.C and then issue a command cl -c TEST.C from the command prompt.....

    I have tried with and without the DEFINE statements, but i still get this message.....


    C:\>cl -c test.c
    Microsoft (R) 32-bit C/C++ Standard Compiler Version 12.00.8168 for 80x86
    Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

    test.c
    test.c(8) : error C2055: expected formal parameter list, not a type list
    test.c(11) : error C2057: expected constant expression
    test.c(11) : error C2466: cannot allocate an array of constant size 0

    C:\>

    I must be doing something daft!

    Pete

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Hmm...try a normal ANSI build...put this code in and build as normal

    Code:
    #include <windows.h>
    
    
    
    int WINAPI WinMain(HINSTANCE hInst,
    				   HINSTANCE,LPSTR,int){
    
    	const int cBuffSize = 255;//Size of buffer
    	CHAR szBuff[cBuffSize] = {0},//Actual buffer
    		  *szPath = //Path of key
    		"SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage";
    	HKEY hKey = NULL;//Key used to hget value
    	DWORD dwSize = cBuffSize; //To tell buff size and recieve actual size
    		 
    
    	//First open a key to allow you to read the registry
    	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,//Main key to browse
    		            szPath,//sub key
    					0,
    					KEY_READ,//access rights - we want to read
    					&hKey)//Recieve the key we want to use
    					!= ERROR_SUCCESS){
    		MessageBox(HWND_DESKTOP,"Could not open key",NULL,MB_OK);
    		return 1;
    	}
    
    	if(RegQueryValueEx(hKey,//From previous call
    					   "ACP",//value we want to look at
    					   0,
    					   NULL,//not needed,we know its a string
    					   (UCHAR*)szBuff,//Put info here
    					   &dwSize)//How big is the buffer?
    					   !=	ERROR_SUCCESS){
    		MessageBox(HWND_DESKTOP,"Could not get value",NULL,MB_OK);
    		return 1;
    	}
    
    	MessageBox(HWND_DESKTOP,szBuff,"Result",MB_OK);
    
    	RegCloseKey(hKey);//Dont forget to cleanup!!!!
    		
    	return 0;
    }

  9. #9
    rintsp
    Guest
    Rob,

    Still no joy....

    C:\>cl -c test.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
    Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

    test.c
    test.c(6) : error C2055: expected formal parameter list, not a type list
    test.c(9) : error C2057: expected constant expression
    test.c(9) : error C2466: cannot allocate an array of constant size 0

    Not sure if there is something I am doing wrong!

    Pete

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789


    Right...I gotcha......My code uses a few of the tricks C++ allows, but are not allowed in straight C
    • I use a const int to size an array - big nono in C
    • I dont explicitly state the params recieved in WinMain - big nono in C
    • I set the array to zero with = {0} - not in C thank you!


    So, compile my code in C++, or do this...

    Code:
    #include <windows.h>
    
    #define cBuffSize 255//Size of buffer
    
    
    
    int WINAPI WinMain(HINSTANCE hInst,
    				   HINSTANCE phInst,LPSTR lps,int nShow){
    
    
    
    
    	CHAR szBuff[cBuffSize],//Actual buffer
    		  *szPath = //Path of key
    		"SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage";
    	HKEY hKey = NULL;//Key used to hget value
    	DWORD dwSize = cBuffSize; //To tell buff size and recieve actual size
    
    
    	//First open a key to allow you to read the registry
    	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,//Main key to browse
    		            szPath,//sub key
    					0,
    					KEY_READ,//access rights - we want to read
    					&hKey)//Recieve the key we want to use
    					!= ERROR_SUCCESS){
    		MessageBox(HWND_DESKTOP,"Could not open key",NULL,MB_OK);
    		return 1;
    	}
    
    	if(RegQueryValueEx(hKey,//From previous call
    					   "ACP",//value we want to look at
    					   0,
    					   NULL,//not needed,we know its a string
    					   (UCHAR*)szBuff,//Put info here
    					   &dwSize)//How big is the buffer?
    					   !=	ERROR_SUCCESS){
    		MessageBox(HWND_DESKTOP,"Could not get value",NULL,MB_OK);
    		return 1;
    	}
    
    	MessageBox(HWND_DESKTOP,szBuff,"Result",MB_OK);
    
    	RegCloseKey(hKey);//Dont forget to cleanup!!!!
    
    	return 0;
    }
    Just woke me up to how un-C-friendly my code has now become....

  11. #11
    Rintsp
    Guest
    Rob,

    Many many thanks!!!

    I should have stated that it had to be C and not C++.

    I used the code in C 10 years ago, and I am very rusty! I have been using Advantage:Gen from CA for ages and the external code we need to write had to be in Ansi C.

    Thank you for the clear code and comments, I think I understand now!!

    Cheers

    Pete

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. How to read a file stream entirely into a vector?
    By jiapei100 in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 03:22 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 03:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21