Thread: Writing registry keys in windows

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Writing registry keys in windows

    I wrote a program and I want to auto run it every time the pc starts, and apparently I have to add a key in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Curr entVersion\Run and HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Run or something like that, and the key value would be the path to the file.

    My question is, how do I write a program that writes the keys automatically? Any sort of windows function that does this?

    Thanks.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Search MSDN via Google for a number of Registry functions. You'll need to call at least three, but probably more along the lines of four for this.

    BTW, as a word of caution, please don't be annoying with this feature/ability. This is something a user could get pretty upset with if it's done without their permission.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    yes I understand about permission stuff, it's just that I'm new to this whole registry thing and I've been researching a bit.

    I think I understand a little bit.
    This: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curr entVersion\Run is called a key (looks like a folder) and it has a lot of values in it (string values) and so what I need (want) to do is put a new value in this key, named HelloWorld with something like "C:\helloworld.exe".

    That value will make this program run every time the computer starts...

    Well thanks I'll keep researching on the registry keys thing.

  4. #4

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are correct.
    If placed under HKEY_LOCAL_MACHINE, it will start for every user (requires Admin privileges).
    If placed under HKEY_CURRENT_USER, it starts for only the current user. Does not require admin privileges.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    What requires admin privileges? Modifying the key?

    And...
    Code:
    LONG WINAPI RegOpenKeyEx(
      __in        HKEY hKey,
      __in_opt    LPCTSTR lpSubKey,
      __reserved  DWORD ulOptions,
      __in        REGSAM samDesired,
      __out       PHKEY phkResult
    );
    I really don't understand these prototypes....
    I'm guessing that the first argument would be
    Code:
    HKEY_LOCAL_MACHINE
    the second
    Code:
    "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
    the third one it says that it must be 0
    the 4th one and the fifth one I have absolutely no Idea... what would I put in them?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by samus250 View Post
    What requires admin privileges? Modifying the key?
    Writing data to HKEY_LOCAL_MACHINE

    the 4th one and the fifth one I have absolutely no Idea... what would I put in them?
    4th one is the access you want to the key.
    Read, write, enumerate, etc.
    Usually, you can just choose the constants KEY_READ or KEY_WRITE.
    The last is the address of a HKEY variable to hold the opened key.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    jeeze, windows programming is another world.

    OK so HKEY is like a typedef to some type of variable.
    And there are constants defined (guess that in windows.h) that are KEY_READ and KEY_WRITE.

    So to open the key that I want I would call
    Code:
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hkeyhandle);
    ? Is that right?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by samus250 View Post
    jeeze, windows programming is another world.
    It truly is.

    OK so HKEY is like a typedef to some type of variable.
    Yes, to some obscure type which we know nothing about, that Windows knows everything about. Our job is just to pass it along to other functions.

    And there are constants defined (guess that in windows.h) that are KEY_READ and KEY_WRITE.
    They are, or should be.

    ? Is that right?
    I think that looks about right. Try the code and see if you get any result.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void) {
        HKEY hkey;
        LONG result_open, result_close;
        
        printf("Opening Key...\n");
        result_open = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                      0, KEY_WRITE, &hkey);
        
        if(result_open != ERROR_SUCCESS) {
            if(result_open  == ERROR_FILE_NOT_FOUND) {
                printf("Not found\n");
            } else {
                printf("Error Opening Key\n");
            }
        } else {
            printf("SUCCESS!!!\n");
        }
        
        printf("Closing Key...\n");
        result_close = RegCloseKey(hkey);
        if(result_close != ERROR_SUCCESS) {
            printf("Error Closing Key\n");
        } else {
            printf("SUCCESS!!!!\n");
        }
        
        return 0;
    }
    It works... now let me see how can I insert a new string value to that key.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void) {
        HKEY hkey;
        LONG result_open, result_write, result_close;
        
        printf("Opening Key...\n");
        result_open = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                      0, KEY_WRITE, &hkey);
        
        if(result_open != ERROR_SUCCESS) {
            if(result_open  == ERROR_FILE_NOT_FOUND) {
                printf("Not found\n");
            } else {
                printf("Error Opening Key\n");
            }
        } else {
            printf("SUCCESS!!!\n");
        }
        
        printf("Writing Value named testval\n");
        result_write = RegSetValueEx(hkey, "testval", 0, REG_SZ, "\"C:\\helloworld.exe\"\0", 20);
        if(result_write != ERROR_SUCCESS) {
            printf("Error Writing Value\n");
        } else {
            printf("SUCCESS!!!\n");
        }
        
        printf("Closing Key...\n");
        result_close = RegCloseKey(hkey);
        if(result_close != ERROR_SUCCESS) {
            printf("Error Closing Key\n");
        } else {
            printf("SUCCESS!!!!\n");
        }
        
        return 0;
    }
    This one works!

    Is there any bad coding practices? Any suggestions (like error checking, I'm really bad at error checking)?

    Thanks!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, for one, if RegSetValueEx fails, you never close the key.
    And the data. Encapsulate it in a const char[] array and use sizeof() for size instead of a magic number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    thanks a lot, I'll fix that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Screensavers for Windows in C++
    By identifier-less in forum C++ Programming
    Replies: 9
    Last Post: 03-31-2006, 02:22 PM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. Reading and writing to the Registry
    By Stan100 in forum Windows Programming
    Replies: 2
    Last Post: 07-29-2004, 12:11 PM
  4. Windows Registry
    By Nor in forum Windows Programming
    Replies: 1
    Last Post: 02-15-2002, 03:19 AM
  5. where windows registry store ?
    By Adisonz in forum C Programming
    Replies: 7
    Last Post: 02-05-2002, 10:41 PM