Thread: How do you use the Reg functions?

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question How do you use the Reg functions?

    Could someone please explain to me how to use these functions? (Examples).

    RegCreateKey( );
    RegLoadKey( );
    RegDeleteKey( );


    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    My suggestion is that if you need to ask, then you need to start with a really good backup of your system.

    Because one code mistake on your part is going to hose your system.

    Most people start with searching say msdn.microsoft.com if they don't have MSDN already installed.

    For sure, I'm sure a google search will show plenty of examples of these functions being used.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I allready tried a google search.

    Quote Originally Posted by Salem
    Because one code mistake on your part is going to hose your system.
    In a nother wards, it is a lot safer to use an ini file than the registry?

  4. #4
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    Sample registry writer/deleter:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int AddRegistryStringValue(HKEY basekey, LPSTR subkey, LPSTR name, LPSTR value)
    {
    	HKEY key;
    	DWORD dis;
    	int ret;
    	
    	/*The disposition DWORD can be either REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY.
    	The former means it didn't exist and was created, the latter means it did exist and was opened*/
    	if(RegCreateKeyEx(basekey, subkey, 
    		0, NULL, 
    		REG_OPTION_NON_VOLATILE, KEY_WRITE, 
    		NULL, &key, &dis) == ERROR_SUCCESS)
    	{
    		if(RegSetValueEx(key, name, 
    			0, REG_EXPAND_SZ, //REG_EXPAND_SZ specifies it is a string type to store such things as paths
    			(LPBYTE)value, 
    			(DWORD)lstrlen(value)+1) == ERROR_SUCCESS) ret = 0;
    		else ret = 2;
    		
    		RegCloseKey(key);
    	}
    	else ret = 1;
    	
    	return ret;
    }
    
    
    int DeleteRegistryStringValue(HKEY basekey, LPSTR subkey, LPSTR name)
    {
    	HKEY key;
    	
    	if(RegOpenKeyEx(basekey, subkey, 
    		0, KEY_WRITE, &key) == ERROR_SUCCESS)
    	{
    		RegDeleteValue(key, name);
    		RegCloseKey(key);
    		return 0;
    	}
    	else return 1;
    }
    
    int main(int argc, char *argv[])
    {
    	if(!AddRegistryStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 
    		"Nothing", argv[0])) printf("Write successful\n");
    	else printf("Write failure\n");
    	
    	if(!DeleteRegistryStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 
    		"Nothing")) printf("Delete Successful");
    	else printf("Delete failure");
    	
    	return 0;
    }

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    And how do I read a key?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    You'll probably want to read up on RegQueryValueEx. A quick and dirty example with a REG_SZ follows.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    #include <windows.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	HKEY myHKEY1 = NULL, myHKEY2 = NULL;
    	DWORD dwType, dwDisposition;
    	BYTE buffer[MAX_PATH];
    	DWORD bufferSize = sizeof(buffer);
    	// just hard coded this so RegOpenKeyEx call isn't so long
    	char szKeyName[ ] = "Software\\Microsoft\\Internet Explorer\\";
    	
    	// Open an existing key
    	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &myHKEY1)== ERROR_SUCCESS)
    		//MessageBox(NULL, "Open", "Title", MB_OK);
    	
    		// query a know value in existing key
    		if(RegQueryValueEx(myHKEY1, "Build", NULL, &dwType, buffer, &bufferSize)==ERROR_SUCCESS)
    			MessageBox(NULL, (char*)buffer, "The Build # is:", MB_OK);
    			
    			// create a new key
    			if(RegCreateKeyEx(HKEY_CURRENT_USER, "Temp", 0, NULL, 0, DELETE | KEY_SET_VALUE, NULL, &myHKEY2, &dwDisposition)== ERROR_SUCCESS)
    				//MessageBox(NULL, "Temp", "Title", MB_OK);
    				
    				// set value in new key from previously query key
    				if(RegSetValueEx(myHKEY2, "Build", 0, dwType, buffer, bufferSize)==ERROR_SUCCESS)
    					//MessageBox(NULL, "Build", "Title", MB_OK);
    					
    					// Clean up, remove testing key
    					if(RegDeleteKey(HKEY_CURRENT_USER, "Temp")==ERROR_SUCCESS)
    						;//MessageBox(NULL, "Delete", "Title", MB_OK);
    		
    	if(myHKEY1)					
    		RegCloseKey(myHKEY1);
    	if(myHKEY2)
    		RegCloseKey(myHKEY2);	
    	
    	cout << "Press `Enter' to continue . . . ";
    	cin.sync();
    	cin.ignore();
    	return(EXIT_SUCCESS);
    }

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM