pprüfen ob ein registry Eintrag existiert

reBourne

Erfahrenes Mitglied
prüfen ob ein registry Eintrag existiert

Wie kann ich überprüfen ,ob ein registry-Schlüssel bzw. Datensatz existiert?
 
Zuletzt bearbeitet:
Versuchen, ihn auszulesen, und wenn RegOpenKey/RegQueryValue fehlschlägt, dann müsste anhand vom Errorcode eigentlich ersichtlich sein, ob das an einem nicht existierenden Key gelegen hat.
Die Errorcodes kannst du in Visual-Studio über das Error-Lookup-Tool erklärt bekommen.
 
Re: prüfen ob ein registry Eintrag existiert

Aha ,da ich c++ grad lerne weiss ich nicht genau wie das geht!

das hier habe ich versucht:
Code:
HKEY hKey = HKEY_LOCAL_MACHINE;
LPCTSTR subkey = "SOFTWARE\\TestKey";
HKEY bufkey;

int err_code = RegOpenKeyEx(hKey, subkey, NULL, KEY_READ, &bufkey);

err_code = RegQueryValueEx(hKey, subkey, NULL, NULL, NULL, NULL);

RegCloseKey(bufkey);

ich kann mit RegOpenKeyEx bzw. RegOpenKey so herausfinden, ob der schlüssel existiert. aber ich möchte eigentlich prüfen, ob in dem schlüssel bestimmte werte vorhanden sind, meinetwegen ein REG_DWORD. aber wie komme ich überhaupt an diese werte heran? das krieg ich irgendwie nicht so ganz hin. kannst du mir vllt mit den parametern für RegQueryValueEx genauer weiterhelfen? ich kenne mich z.B. nicht so gut mit den char variablen aus... gibt ja so viele möglichkeiten, als array, als unsigned char, dann noch mit *... was mir schon große schwierigkeiten bereitet. für einen code der mir in einen char einen string oder int aus der registry lesen kann wäre ich dankbar.
 
moin


In der MSDN steht das alles sehr ausführlich (http://www.msdn.microsoft.com):
Code:
RegOpenKeyEx

The RegOpenKeyEx function opens the specified registry key.


LONG RegOpenKeyEx(
  HKEY hKey,
  LPCTSTR lpSubKey,
  DWORD ulOptions,
  REGSAM samDesired,
  PHKEY phkResult
);

Parameters
hKey 
[in] Handle to an open key. This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys: 

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS




Windows Me/98/95:  This parameter can also be the following key: 


HKEY_DYN_DATA





lpSubKey 
[in] Pointer to a null-terminated string containing the name of the subkey to open. If this parameter is NULL or a pointer to an empty string, the function will open a new handle to the key identified by the hKey parameter. In this case, the function will not close the handles previously opened. 
For more information, see Registry Element Size Limits.

ulOptions 
Reserved; must be zero. 
samDesired 

[in] Access mask that specifies the desired access rights to the key. The function fails if the security descriptor of the key does not permit the requested access for the calling process. For more information, see Registry Key Security and Access Rights. 
phkResult 

[out] Pointer to a variable that receives a handle to the opened key. When you no longer need the returned handle, call the RegCloseKey function to close it. 
Return Values
If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.

Code:
RegQueryValueEx

The RegQueryValueEx function retrieves the type and data for a specified value name associated with an open registry key.


LONG RegQueryValueEx(
  HKEY hKey,
  LPCTSTR lpValueName,
  LPDWORD lpReserved,
  LPDWORD lpType,
  LPBYTE lpData,
  LPDWORD lpcbData
);

Parameters
hKey 
[in] Handle to an open key. The key must have been opened with the KEY_QUERY_VALUE access right. For more information, see Registry Key Security and Access Rights. 
This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys:


     HKEY_CLASSES_ROOT
     HKEY_CURRENT_CONFIG
     HKEY_CURRENT_USER
     HKEY_LOCAL_MACHINE
     HKEY_PERFORMANCE_DATA
     HKEY_PERFORMANCE_NLSTEXT
     HKEY_PERFORMANCE_TEXT
     HKEY_USERS




Windows Me/98/95:  This parameter can also be the following key: 


HKEY_DYN_DATA





lpValueName 
[in] Pointer to a null-terminated string containing the name of the value to query. 
If lpValueName is NULL or an empty string, "", the function retrieves the type and data for the key's unnamed or default value, if any.

For more information, see Registry Element Size Limits.


Windows Server 2003, Windows XP/2000/NT:  Keys do not automatically have an unnamed or default value. Unnamed values can be of any type.



Windows Me/98/95:  Every key has a default value that initially does not contain data. On Windows 95, the default value type is always REG_SZ. On Windows 98 and Windows Me, the type of a key's default value is initially REG_SZ, but RegSetValueEx can specify a default value with a different type.


lpReserved 
Reserved; must be NULL. 
lpType 

[out] Pointer to a variable that receives a code indicating the type of data stored in the specified value. For a list of the possible type codes, see Registry Value Types. The lpType parameter can be NULL if the type code is not required. 
lpData 

[out] Pointer to a buffer that receives the value's data. This parameter can be NULL if the data is not required. 
lpcbData 

[in, out] Pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. When the function returns, this variable contains the size of the data copied to lpData. 
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then lpcbData will also include the size of the terminating null character or characters.

The lpcbData parameter can be NULL only if lpData is NULL.

If the buffer specified by lpData parameter is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of the lpData buffer are undefined.

If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This enables an application to determine the best way to allocate a buffer for the value's data.

If hKey specifies HKEY_PERFORMANCE_DATA and the lpData buffer is not large enough to contain all of the returned data, RegQueryValueEx returns ERROR_MORE_DATA and the value returned through the lpcbData parameter is undefined. This is because the size of the performance data can change from one call to the next. In this case, you must increase the buffer size and call RegQueryValueEx again passing the updated buffer size in the lpcbData parameter. Repeat this until the function succeeds. You need to maintain a separate variable to keep track of the buffer size, because the value returned by lpcbData is unpredictable.

Return Values
If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.


mfg
umbrasaxum
 
ja doll daher hab ich auch alles was ich bisher fabriziert hab. aber mehr krieg ich nicht so wirklich hin -.- dazu fehlt mir einiges an basiswissen, was mir die arbeit mit MSDN recht erschwert.
 
moin


OK dann werde ich kurz die Parameter erklären.

Code:
DWORD lpcbData;
DWORD lpType;
BYTE Data;


RegQueryValueEx(hKey, lpValueName, NULL, lpType, Data, lpcbData );

Parameter 1 und 2 hast du ja schon.
Parameter 3 muss NULL sein.
Parameter 4 hat nach aufrufen von RegQueryValueEx einen Wert, der den Typ der Variable in der Registry angibt. In Der Liste "Registry Value Types" steht welcher Wert welchen Wert für welchen Typ steht.
Parameter 5 hier wird der Wert in der entsprechenden Variable aus der Registry gespeichert.
Parameter 6 gibt die nach aufrufen von RegQueryValueEx an wie groß Data ist.


Vielleicht solltest du dir erstmal das Grundwissen aneignen, bevor du dich an solch komplexe Sachen wagst.


mfg
umbrasaxum
 
Zurück