C++ 检查注册表项是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1343577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a registry key exists
提问by slicedlime
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.
我正在寻找一种干净的方法来检查注册表项是否存在。我原以为RegOpenKey
如果我试图打开一个不存在的密钥会失败,但事实并非如此。
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.
我猜你可以RegQueryInfoKey
以某种方式使用它,但 MSDN 没有提供太多关于如何使用的细节,即使它是可能的。
Update: I need the solution in Win32 api, not in managed code, .NET or using any other library.
更新:我需要 Win32 api 中的解决方案,而不是托管代码、.NET 或使用任何其他库。
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:
MSDN 中的文档似乎表明您应该能够打开一个密钥以获得读取权限并在它不存在时得到一个错误,如下所示:
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.
但是,ERROR_SUCCESS
当我尝试这个时,我明白了。
Update 2: My exact code is this:
更新 2:我的确切代码是这样的:
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.
...但是result
是ERROR_SUCCESS
,即使我试图打开一个不存在的密钥。
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.
更新 3:看起来你们是对的。这在一个特定的测试示例上失败(神秘地)。如果我在任何其他键上尝试它,它会返回正确的结果。用注册表编辑器仔细检查它仍然没有显示密钥。不知道该怎么办。
回答by Byron Whitlock
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.
首先,不要担心像这样的东西的性能。除非您每秒查询 100 次,否则它会足够快。过早的优化会让你头疼。
RegOpenKeyEx will return ERROR_SUCCESS if it finds the key. Just check against this constant and you are good to go.
如果找到密钥,RegOpenKeyEx 将返回 ERROR_SUCCESS。只需检查这个常数,你就可以开始了。
回答by 1800 INFORMATION
RegOpenKey
does return an error if the key does not exist. How are you using it? The expected return value is ERROR_FILE_NOT_FOUND
.
RegOpenKey
如果键不存在,则返回错误。你如何使用它?预期的返回值为ERROR_FILE_NOT_FOUND
。
From your code:
从您的代码:
HKEY subKey = nullptr;
LONG result = RegOpenKeyEx(key, subPath.c_str(), 0, KEY_READ, &subKey);
if (result != ERROR_SUCCESS) {
I would look at the value of key
and subPath
and make sure they are what you expect, and that the key does not actually exist. What is the value of subKey
afterwards? It is obviously opening something - try enumerating it to see what the keys and values under it are.
我会查看key
and的值并subPath
确保它们是您所期望的,并且密钥实际上并不存在。subKey
之后的价值是什么?它显然是在打开一些东西 - 尝试枚举它以查看它下面的键和值是什么。
There is no issue with RegOpenKey
not returning an error if the key does not exist - I would not try to assume there is some kind of weird OS bug in something as commonly used as the registry.
RegOpenKey
如果密钥不存在,则不返回错误是没有问题的- 我不会尝试假设在注册表这样常用的东西中存在某种奇怪的操作系统错误。
Maybe you have a registry key that is not visible to you, the user that is running the registry editor, but not to your code? A permissions problem perhaps? Is your code running as an elevated user in windows Vista or server 2008? Did you try running the registry editor as administrator?
也许您有一个对您(运行注册表编辑器的用户)不可见的注册表项,但对您的代码不可见?也许是权限问题?您的代码是否在 Windows Vista 或 server 2008 中以提升用户身份运行?您是否尝试以管理员身份运行注册表编辑器?
回答by MSalters
Note that beside the "core" Registry functions that start with "Reg" there are also helper functions starting with "SHReg". These are intended for use by the Shelli.e. Explorer but are documented and can be used in normal applications too. They're typically thin wrappers that make some common tasks easier. They're part of the "Shell LightWeight API" (shlwapi.dll)
请注意,除了以“Reg”开头的“核心”注册表函数之外,还有以“SHReg”开头的辅助函数。这些旨在供Shellie Explorer使用,但已记录在案,也可用于普通应用程序。它们通常是使一些常见任务更容易的薄包装器。它们是“Shell LightWeight API”(shlwapi.dll)的一部分