Thread: Registry Editing

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Registry Editing

    I've searched every thread in the board and hit MSDN for this for hours, but I still cannot solve my problem. I am aiming to create a registry key in the Run dir, or simply to make my program run each time on boot. To do this, I need to RegCreateKeyEx, then to RegSetValueEx, to set the value of this key, however none of this is working. This is the little I have so far :

    Code:
    HKEY HandleKey = NULL;
    HKEY* HandleKey2 = NULL;
    char *tryme = "C:\\Docs\\bsuv.exe";
    
            RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, 
    "REG_SZ", REG_OPTION_NON_VOLATILE, 0, NULL, HandleKey2, 
    NULL);
            RegCloseKey(HandleKey);
            RegSetValueEx(HandleKey, "BSUV", 0, REG_SZ, (BYTE*)tryme,
     29);
    As you see, it's a bunch of nothing. I tried many combinations, but I can't get the BYTE to work in RegSetValueEx, and the RegCreateKeyEx doesn't want to work. As well as that, I'm not using my handle to my key, HKEY HandleKey, but a pointer to it, HandleKey2... If anyone could enlighten me, I'd be very grateful.
    Thanks,
    Korhedron
    Last edited by Korhedron; 04-25-2004 at 10:55 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Few things...try to open the key instead of re-creating it...if you are opening 1 key, you need 1 handle - not 2...you open a key with 1 handle, then close on a NULL-handle and try to use it??...Always check your errors!!!

    Here's an example using the windows calculator

    Code:
    #include <windows.h>
    #include <string>
    #include <iostream>
    
    int main()	
    {
    	HKEY hKey = 0;
    	const std::string strPath = "C:\\WINNT\\system32\\calc.exe";
    
        if(RegOpenKeyEx(HKEY_CURRENT_USER,
    		"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, 
    		KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
    	{
            if(RegSetValueEx(hKey, "CALC", 0, REG_SZ,reinterpret_cast<const BYTE*>(strPath.c_str()),
    			strPath.length() + 1) == ERROR_SUCCESS)
    		{
    			std::cout << "Success!";
    		}
    		else
    		{
    			std::cout << "Could not set value";
    		}
    		RegCloseKey(hKey);
    	}
    	else
    	{
    		std::cout << "Could not open key";
    	}
    	
    
    }

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thank you for your reply. I have a few questions :

    Why use RegOpenKeyEx if my key doesn't exist ?
    Why are you using a reference to the key ( &hKey ) in RegOpenKeyEx ?
    Is there no other way than to use <reinterpret_cast> in RegSetValueEx ?

    Otherwise, thank you for your code
    Korhedron

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    1 The "Run" key already exists...you are just opening it and adding a value for your app.
    2 You pass that pointer to get a handle to the key you are opening. Without that you cant address the open key.
    3 Yeah...just use a standard C type cast - (const BYTE *)...but I like to use the C++ casts whenver I code - preference!

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    RegCreateKeyEx() returns ERROR_SUCCESS if it works, otherwise it returns the reason for failing, you should retrieve and examine this status. I suspect it will tell you that the last parameter should be the address of a variable to return it's action code in, and not NULL.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thank you very much for your replies

    Korhedron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Editing Windows registry
    By thebrighter in forum Windows Programming
    Replies: 2
    Last Post: 07-12-2007, 05:25 PM
  2. Editing the registry using C++
    By axr0284 in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2004, 02:20 PM
  3. registry editing for creating files
    By jverkoey in forum Windows Programming
    Replies: 2
    Last Post: 04-04-2003, 07:43 PM
  4. Editing Windows Registry in C?
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 01-11-2002, 09:42 PM