PDA

View Full Version : check windows registry key existence



mentalmushroom
4th April 2011, 11:57
Hi there! I'd like to know whether it is possible to find out if a registry key exists by the means of QSettings. I was trying to use QSettings::contains, but it doesn't work.



QSettings reg("HKEY_LOCAL_MACHINE\\Software\\VideoLan", QSettings::NativeFormat);
bool exists = reg.contains("VLC");

JohannesMunk
4th April 2011, 13:30
Could it be that "VLC" is a subkey and not a value? Does this work?


QSettings reg("HKEY_LOCAL_MACHINE\\Software\\VideoLan\\VLC", QSettings::NativeFormat);
bool exists = reg.contains("Default");


Joh

mentalmushroom
5th April 2011, 10:04
Yes, VLC is a subkey and not a value. But I thought QSettings::contains is intended to be used for keys. Your code works, but I can't check key the way you said, because actually I don't need registry keys to be created (QSettings will create key if "HKEY_LOCAL_MACHINE\\Software\\VideoLan\\VLC" doesn't exist), therefore I need to use somethings like:


QSettings reg("HKEY_LOCAL_MACHINE", QSettings::NativeFormat);
bool exists = reg.contains("Software\\VideoLan\\VLC");

or


QSettings reg("HKEY_LOCAL_MACHINE", QSettings::NativeFormat);
bool exists = reg.contains("Software\\VideoLan\\VLC\\Default");

Or I don't know how to do it in a better way. But anyway this doesn't work.

JohannesMunk
5th April 2011, 10:48
Well no idea. As this is highly platform dependent anyway, why don't you use winapi directly?



#ifdef Q_OS_WIN
#include "windows.h"
#endif

bool existsAndSuccess = false;
#ifdef Q_OS_WIN
HKEY hKey;
existsAndSuccess = (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\VideoLan\\VLC", 0, KEY_READ, &hKey) == ERROR_SUCCESS);
#endif
qDebug() << existsAndSuccess;
Maybe somebody else knows how to do this with QSettings properly?

Joh

mentalmushroom
5th April 2011, 15:15
yes, i ended up with this approach. but later i use qsettings anyway (because it is easier), therefore i thought to use qsettings for this check also and, generally, i was interested whether it is possible. ok, thank you.