RegGetValue 问题
我正在尝试用 C++ 编写(我认为是)一个简单的脚本来搜索注册表(特别是 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall)并返回 DisplayName 值。
我已经浏览了 MSDN 文档,并在谷歌上进行了数小时的搜索,不幸的是我被困住了。
#define BUFFER 8192
char value[255];
DWORD BufferSize = BUFFER;
if(RegGetValue(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"),
_T("DisplayName"),
RRF_RT_ANY,
NULL,
(PVOID)&value,
&BufferSize)
)
{
_tprintf(TEXT("(%d) %s - %s\n"), i+1, achKey, value);
}
现在,我需要能够将 achKey 附加到 RegGetValue 的第二个参数,以便它在循环每个子项时获取正确的值。
我已经尝试过一百万种不同的东西,不幸的是我在 C++ 方面的经验非常有限,而且我的 google 技能显然也需要一些改进。
编辑: achKey 是密钥的名称: 例如:NVIDIA 驱动程序
因此,附加时,第二个参数应为:
SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NVIDIA Drivers
这是关于 RegGetValue 的 MSDN 参考: http://msdn.microsoft.com/en -us/library/ms724868%28v=vs.85%29.aspx
我也尝试过类似的操作:
wcscat(_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"), achKey)
它会编译,但运行时会崩溃。
I'm trying to write(what I thought would be) a simple script in C++ to search through the registry(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall specifically) and return the value of the DisplayName value.
I have gone through the MSDN docs, and hours of searching on google, unfortunately I'm stuck.
#define BUFFER 8192
char value[255];
DWORD BufferSize = BUFFER;
if(RegGetValue(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"),
_T("DisplayName"),
RRF_RT_ANY,
NULL,
(PVOID)&value,
&BufferSize)
)
{
_tprintf(TEXT("(%d) %s - %s\n"), i+1, achKey, value);
}
Now, I need to be able to append achKey to the 2nd parameter of RegGetValue, so that it grabs the correct values when looping through each subkey.
I've tried a million different things, unfortunately my experience in C++ is pretty limited and my google skills apparently need some work as well.
Edit:
achKey is the name of the key:
Ex: NVIDIA Drivers
So when appended, the 2nd param should read:
SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NVIDIA Drivers
Here's the MSDN reference on RegGetValue:
http://msdn.microsoft.com/en-us/library/ms724868%28v=vs.85%29.aspx
I have also tried something like:
wcscat(_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"), achKey)
It will compile, but then when run, it crashes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以看到您的原始代码有两个主要问题:
char 值[255]
。使用wchar_t
或TCHAR
而不是char
。RegGetValue()
函数将自动“转发”到RegGetValueW()
或RegGetValueA()
函数,具体取决于项目 Unicode 设置。如果您希望强制使用特定字符集,可以直接使用这些函数,但通常最好直接使用RegGetValue()
函数。以下代码是按照您想要的方式使用宽字符串的示例:
编辑:LPCWSTR 注释
Windows 中的
LPCWSTR
是一个指向常量宽字符串的简单指针,或者更直接的是const wchar_t *
,它与 Unicode 项目中的TCHAR *
相同。请注意,如果您将项目更改为多字节字符集,则RegGetValue()
的函数声明(以及许多其他 Windows 函数)将更改为使用LPCSTR
TCHAR
只是一个char
。使用 std::string/wstring 的好处是它们直接与
LPCWSTR
和LPCSTR
兼容。因此,您对 RegGetValue() 的调用可以直接使用 std::wstring 变量,如下所示:There are two main issues with your original code that I can see:
char value[255]
. Use awchar_t
orTCHAR
instead ofchar
. theRegGetValue()
function will be automatically 'forwarded' to theRegGetValueW()
orRegGetValueA()
functions depending on the project Unicode settings. If you wish to force a particular character set you can use those functions directly but it is generally better to just use theRegGetValue()
function directly.The following code is an example of using wide-strings in the manner you desire:
Edit: LPCWSTR Notes
A
LPCWSTR
in Windows is simple a pointer to a constant wide string, or more directly aconst wchar_t *
which is the same thing as aTCHAR *
in a Unicode project. Note that if you changed your project to a MultiByte character set then the function declaration ofRegGetValue()
(and a host of other Windows functions) would change to using aLPCSTR
instead and aTCHAR
would simply be achar
.The nice thing about using std::string/wstring is that they are directly compatible with a
LPCWSTR
and aLPCSTR
. Thus your call toRegGetValue()
can use the std::wstring variables directly like:这是因为您试图将某些内容连接到文字字符串上,因此最好的情况是您会破坏应用程序的随机区域。我有点惊讶它编译时没有错误或警告,因为文字字符串将是 const...?
http://msdn.microsoft.com/en -us/library/h1x0y282%28v=vs.80%29.aspx
That's because you are trying to concatenate something onto a literal string, so the best that will happen is you will trample over random areas of your application. I'm a little surprised it compiles without errors or warnings, as the literal string would be const...?
http://msdn.microsoft.com/en-us/library/h1x0y282%28v=vs.80%29.aspx
您在这里看到的是非常基本的字符串操作。例如:
如果您使用宽字符串,则需要使用
wstring
(和宽字符文字):What you're looking at here is pretty basic string manipulation. Something like:
If you're using wide strings, you want to use
wstring
(and wide character literals) instead: