Reading a value from a C ++ registry

I want to get the path from where the application is installed. There is an entry in the registry that gives the path to my application, see this screenshot: http://i56.tinypic.com/2ly1l6s.jpg

I want to read the path where my application is located. In other words, I need part of 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 does not work, unwanted characters are printed. Thank you very much.

+5
source share
4 answers

you are using a version other than UNICODE, o RegQueryValueEx, and you are duplicating it with the wide format version of printf. Use either printf or change to wprintf (L "% S", mydata)

: RegQueryValueEx (HKEY_CURRENT_USER,...) RegQueryValueEx (hKey,...)

+5

:

  • _T()
  • RegQueryValueEx hKey

RegQueryValueEx . ...

+1

, RegQueryValueEx?

. . , . Unicode RegQueryValueEx L"TestApplication" _T("TestApplication") TEXT("TestApplication"). RegQueryValueEx - typedef RegQueryValueExA RegQueryValueExW, , Unicode .

.

+1
source

The main problem with the code you submitted is that you have a C-style. Each actor is an attractor of errors. And in fact, some of your drives are errors (hiding that you are using incompatible character types).

I want to read the path where my application is located.

Use GetModuleFileName.

MSDN docs :
Get a fully qualified path

Cheers and hth.,

0
source

All Articles