简体   繁体   English

从注册表 C++ 读取值

[英]Reading a value from the registry C++

I want to get the path of where an application is installed.我想获取安装应用程序的路径。 In the registry, there is an entry which gives the path of my application, see this screenshot: http://i56.tinypic.com/2ly1l6s.jpg在注册表中,有一个条目给出了我的应用程序的路径,请参见此屏幕截图: http://i56.tinypic.com/2ly1l6s.jpg

I want to read the path where my application is located.我想读取我的应用程序所在的路径。 In other words, I want the C:\Projects\MyApplication\MyApplication.exe part.换句话说,我想要 C:\Projects\MyApplication\MyApplication.exe 部分。 Here is what I am trying to do:这是我正在尝试做的事情:

HKEY hKey;
wchar_t mydata[2048];
DWORD dataLength = sizeof(mydata);
DWORD dwType = REG_SZ;
LPVOID messagecaliss;
LONG regOpenCriss = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\", 0, KEY_QUERY_VALUE, &hKey);
GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, GetLastError(), NULL,(LPTSTR) &messagecaliss, 0, NULL );
if (regOpenCriss == ERROR_SUCCESS) {
RegQueryValueEx(HKEY_CURRENT_USER, "TestApplication", 0, &dwType, (BYTE*)mydata, &dataLength);
wprintf(L"%s\n", mydata);
system("PAUSE");
}
else
    MessageBox(NULL,(LPCTSTR)messagecaliss,"ERROR",MB_OK|MB_ICONINFORMATION);

This doesn't work, junk characters are printed.这不起作用,会打印垃圾字符。 Thank you very much.非常感谢。

you're using non-UNICODE version o RegQueryValueEx and you're diplaying it with wide-char version of printf.您正在使用非 UNICODE 版本 o RegQueryValueEx,并且正在使用 printf 的宽字符版本来显示它。 Use either printf or change to wprintf( L"%S",mydata )使用 printf 或更改为 wprintf( L"%S",mydata )

Note: RegQueryValueEx(HKEY_CURRENT_USER,... ) must be RegQueryValueEx( hKey,... )注意:RegQueryValueEx(HKEY_CURRENT_USER,...) 必须是 RegQueryValueEx(hKey,...)

I got results after:之后我得到了结果:

  1. I surround the strings with _T()我用 _T() 包围字符串
  2. I call RegQueryValueEx with hKey as the first parameter我用hKey作为第一个参数调用RegQueryValueEx

You should store the result of RegQueryValueEx in a variable and check it.您应该将RegQueryValueEx的结果存储在一个变量中并检查它。 Handle the failure case...处理失败案例...

This doesn't work这不起作用

How do you know that without checking the return value of RegQueryValueEx ?如果不检查RegQueryValueEx的返回值,你怎么知道?

junk characters are printed打印垃圾字符

No. It's not junk.不,这不是垃圾。 You didn't ask for a wide character string, so you cannot expect to get one.你没有要求一个宽字符串,所以你不能指望得到一个。 Compile with Unicode enabled and call RegQueryValueEx with L"TestApplication" or _T("TestApplication") or TEXT("TestApplication") .在启用 Unicode 的情况下编译并使用L"TestApplication"_T("TestApplication")TEXT("TestApplication")调用RegQueryValueEx RegQueryValueEx is just a typedef for RegQueryValueExA or RegQueryValueExW , depending on whether Unicode is defined during compile time or not. RegQueryValueEx只是RegQueryValueExARegQueryValueExW的 typedef,具体取决于 Unicode 是否在编译时定义。

Thank you very much非常感谢

You're welcome.别客气。

A main problem with the code you present is that you have C style casts.您提供的代码的一个主要问题是您有 C 样式转换。 Each cast is a bug attractor.每个演员都是一个虫子吸引器。 And in fact, some of your casts are bugs (hiding that you're using incompatible character types).事实上,你的一些演员是错误(隐藏你使用不兼容的字符类型)。

I want to read the path where my application is located.我想读取我的应用程序所在的路径。

Use GetModuleFileName .使用GetModuleFileName

MSDN docs : MSDN 文档
Retrieves the fully-qualified path检索完全限定的路径

Cheers & hth.,干杯&hth.,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM