-
March 10th, 2023, 11:37 PM
#1
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.
-
March 11th, 2023, 03:19 AM
#2
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
-
March 11th, 2023, 06:20 AM
#3
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.2)
-
March 11th, 2023, 02:36 PM
#4
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.
-
March 11th, 2023, 02:59 PM
#5
Re: Registry - Check for key or value Hello, I am trying to check for the existance
 Originally Posted by NewPlaza
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
-
March 12th, 2023, 05:38 AM
#6
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.2)
-
March 12th, 2023, 01:35 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|