CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Dec 2020
    Posts
    11

    Resolved Registry - Check for key or value Hello, I am trying to check for the existance of

    Registry - Check for key or value

    Hello,

    I am trying to check for the existance of a key or value in the registry.
    I know how to write the values,
    Code:
    RegSetKeyValue(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "StartupBuddy", REG_SZ, "0", 1);
    Code:
    RegSetKeyValue(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "StartupBuddy", REG_SZ, "1", 1);


    Code:
    CreateWindow(TEXT("BUTTON"),// * GroupBox
     TEXT("Startup Settings"),
     WS_VISIBLE | WS_CHILD | BS_GROUPBOX | BS_CENTER,
     10, // Start Left
     10, // Start Top
     WidthX - 25,
     50,
     hwnd,
     (HMENU) ID_GroupBox_StartupSettings,
     NULL,
     NULL);
    					
    CreateWindow(TEXT("BUTTON"),// * CheckBox
     TEXT("Start with Windows"),
     WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
     30, // Start Left
     25, // Start Top
     150,
     30,
     hwnd,
     (HMENU) ID_CheckBox_WindowsStartup,
     NULL,
     NULL);


    But do not know how to check for them
    check for value in registry, if found
    Code:
    CheckDlgButton(hwnd, ID_CheckBox_WindowsStartup, BST_CHECKED);
    Thank you.
    Last edited by NewPlaza; March 12th, 2023 at 01:35 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Registry - Check for key or value Hello, I am trying to check for the existance

    Have a loo at RegEnumKey and RegEnumValue functions.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Registry - Check for key or value Hello, I am trying to check for the existance

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Dec 2020
    Posts
    11

    Re: Registry - Check for key or value Hello, I am trying to check for the existance

    Thank you for your replies.
    2kaud, I actually have tried RegGetValue() but have issues using it. I also tried RegQueryValue() with limited success.

    My code for that statement.
    Code:
    char szPath[MAX_PATH];
    LONG cbValue=sizeof(szPath);
    if(!RegQueryValue(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "StartupBuddy", &cbValue)==ERROR_SUCCESS){
    printf( "StartupBuddy key not found. App not set to boot with Windows.\n" );
    // Do nothing.....}
    else {
    printf( "StartupBuddy key found. App is set to boot with Windows.\n" );
    CheckDlgButton(hwnd, ID_CheckBox_WindowsStartup, BST_CHECKED);//Check "Start with Windows" checkbox.
    }
    It works only when the key isn't found in the registry. BUT will crash(exits prematurely or unexpected) when the key already exists.
    Thanks.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Registry - Check for key or value Hello, I am trying to check for the existance

    Quote Originally Posted by NewPlaza View Post
    My code for that statement.
    Code:
    char szPath[MAX_PATH];
    LONG cbValue=sizeof(szPath);
    if (!RegQueryValue(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "StartupBuddy", &cbValue)==ERROR_SUCCESS)
    {
             printf( "StartupBuddy key not found. App not set to boot with Windows.\n" );
             // Do nothing....
    }
    Try:
    Code:
    char szPath[MAX_PATH];
    LONG cbValue=sizeof(szPath);
    LSTATUS errorCode = RegQueryValue(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "StartupBuddy", &cbValue);
    if (errorCode != ERROR_SUCCESS)
    {
             printf( "StartupBuddy key not found. App not set to boot with Windows.\n" );
             // Do nothing....
    }
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Registry - Check for key or value Hello, I am trying to check for the existance

    You are using RegQueryValue() incorrectly. The first param is the key to be used (already opened), the second is the value to be retrieved, the 3rd the address of where to place the retrieved data and the 4th a pointer to a value that has the maximum number of bytes that can be retrieved. But in this case you want RegGetValue(). Something like (NOT tried):

    Code:
    char szPath[MAX_PATH] = {0};
    DWORD cbValue=sizeof(szPath);
    if (RegGetValue(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "StartupBuddy", RRF_RT_REG_SZ, NULL, szPath, &cbValue) != ERROR_SUCCESS) {
        printf( "StartupBuddy key not found. App not set to boot with Windows.\n" );
        // Do nothing.....
    } else {
        printf( "StartupBuddy key found. App is set to boot with Windows.\n" );
        CheckDlgButton(hwnd, ID_CheckBox_WindowsStartup, BST_CHECKED);    //Check "Start with Windows" checkbox.
    }
    Note that cbValue is of type DWORD and not LONG.

    See https://learn.microsoft.com/en-us/wi...g-reggetvaluea

    Note. If you just want to display a string ending with a \n it's easier to use puts()

    Code:
    puts( "StartupBuddy key not found. App not set to boot with Windows." );
    Last edited by 2kaud; March 12th, 2023 at 05:56 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Dec 2020
    Posts
    11

    Resolved Re: Registry - Check for key or value Hello, I am trying to check for the existance

    2kaud,

    Awesome! That worked! Thank you so much!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured