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
I want to read the path where my application is located. In other words, I want the C:\Projects\MyApplication\MyApplication.exe part. 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. Use either printf or change to wprintf( L"%S" ,mydata )
Note : RegQueryValueEx(HKEY_CURRENT_USER ,... ) must be RegQueryValueEx( hKey ,... )
I got results after:
RegQueryValueEx
with hKey
as the first parameterYou should store the result of RegQueryValueEx
in a variable and check it. Handle the failure case...
This doesn't work
How do you know that without checking the return value of 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")
. RegQueryValueEx
is just a typedef for RegQueryValueExA
or RegQueryValueExW
, depending on whether Unicode is defined during compile time or not.
Thank you very much
You're welcome.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With