Hi, I'm trying to read an install path from the registry and use this to check the existence of a set of files in the file system. Here's is my code (modified for this post):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
struct stat stFileInfo;
HKEY keyHandle;
DWORD dataType = REG_SZ;
DWORD buffer;
char regValue[1024];
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MyApp"),0, KEY_READ, &keyHandle) == ERROR_SUCCESS)
{
buffer = 1023;
if(RegQueryValueEx( keyHandle, TEXT("InstallLocation"), NULL, &dataType, (LPBYTE)regValue, &buffer) == ERROR_SUCCESS)
{
MessageBox(NULL, (LPCWSTR)regValue, TEXT("Value found"), MB_OK);
string fileToCheck = string(regValue);
fileToCheck.append("MyApp.exe");
if(stat(fileToCheck.c_str(),&stFileInfo) != 0)
{
//MyApp.exe does not exist in file system
}
}
else
{
//handle fail to read reg value
}
}
else
{
//handle non-existent or failed to open reg key
}
RegCloseKey(keyHandle);
|
The MessageBox line shows the correct install path. However the appended path contains just the first letter of the install path retrieved from the registry plus the .exe name.
Why is this? I understand that char is really an array type, but various posts show this
string fileToCheck = string(regValue);
as being one way
of converting the contents of char array to string.
I've tried defining regValue as string, however the returned registry value is truncated after 20 chars and has an additioanl 'B' appended. Again why is this? I've also tried defining regValue as wchar_t and using RegQueryValueExA() as per some suggestions.
How can I extract the install path, append the name of a file to it and then check if this file exists?
I'm using VisualStudio 2010 Express (compiling with the default Configuration Options>Character Set = Use Unicode Character Set) and using the FireBreath framework to develop a browser plugin which outputs a dll. Part of my problem is not being able to easily view debug output after each line of code, so I can see where things are failing. I can't attach a debugger to the dll (VS Express limitations). The FireBreath framework provides a method for outputting to the browser debug console, but this takes a string value, so for example I can't output the sizeof(regValue). What methods can I use to debug?
As an aside why do I need TEXT("") in the registry key functions? The examples on MSDN don't use this??