Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 341697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:43:59+00:00 2026-05-12T10:43:59+00:00

I am looking for a clean way to check if a registry key exists.

  • 0

I am looking for a clean way to check if a registry key exists. I had assumed that RegOpenKey would fail if I tried to open a key that didn’t exist, but it doesn’t.

I could use string processing to find and open the parent key of the one I’m looking for, and then enumerate the subkeys of that key to find out if the one I’m interested in exists, but that feels both like a performance hog and a weird way to have to implement such a simple function.

I’d guess that you could use RegQueryInfoKey for this somehow, but MSDN doesn’t give too many details on how, even if it’s possible.

Update: I need the solution in Win32 api, not in managed code, .NET or using any other library.

The docs in MSDN seem to indicate that you should be able to open a key for read permission and get an error if it doesn’t exist, like this:

lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);

if (lResult != ERROR_SUCCESS) 
{
    if (lResult == ERROR_FILE_NOT_FOUND) {

However, I get ERROR_SUCCESS when I try this.

Update 2: My exact code is this:

HKEY subKey = nullptr;
LONG result = RegOpenKeyEx(key, subPath.c_str(), 0, KEY_READ, &subKey);
if (result != ERROR_SUCCESS) {

… but result is ERROR_SUCCESS, even though I’m trying to open a key that does not exist.

Update 3: It looks like you guys are right. This fails on one specific test example (mysteriously). If I try it on any other key, it returns the correct result. Double-checking it with the registry editor still does not show the key. Don’t know what to make of all that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:43:59+00:00Added an answer on May 12, 2026 at 10:43 am

    First of all don’t worry about performance for stuff like this. Unless you are querying it 100x per sec, it will be more than fast enough. Premature optimization will cause you all kinds of headaches.

    RegOpenKeyEx will return ERROR_SUCCESS if it finds the key. Just check against this constant and you are good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a clean way to combine variables into a single string
I'm looking for a clean way to import one git repository from another, where
I've been looking at a clean way of implementing shut down logic for a
I am looking for a neat/clean way to store a list of strings into
I was looking for a reliable, clean way to do advanced runtime reports for
I'm looking for a way to automatically do some clean up tasks when the
I'm looking for a fast, clean, pythonic way to divide a list into exactly
I'm looking for a way to validate all the data on a form that
I'm looking for a way to convert a string that contains a character escape
I'm looking for a way to check if the default browser has a tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.