Sto provando a creare una nuova chiave di registro nel registro di Windows utilizzando C++. Ecco il codice che ho finora:Creare una nuova chiave di registro Windows utilizzando C++
HKEY hKey;
LPCTSTR sk = TEXT("SOFTWARE\\OtherTestSoftware");
LONG openRes = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
sk,
0,
NULL,
REG_OPTION_BACKUP_RESTORE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
if (openRes==ERROR_SUCCESS) {
printf("Success creating key.");
} else {
printf("Error creating key.");
}
LPCTSTR value = TEXT("OtherTestSoftwareKey");
LPCTSTR data = "OtherTestData\0";
LONG setRes = RegSetValueEx (hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if (setRes == ERROR_SUCCESS) {
printf("Success writing to Registry.");
} else {
printf("Error writing to Registry.");
}
//RegDeleteKey(hKey, sk);
LONG closeOut = RegCloseKey(hKey);
if (closeOut == ERROR_SUCCESS) {
printf("Success closing key.");
} else {
printf("Error closing key.");
}
sono in grado di aprire con successo una chiave esistente utilizzando un codice molto simile frammento (in pratica sostituire RegCreateKeyEx con RegOpenKeyEx). Immagino che uno o più degli argomenti che sto passando in RegCreateKeyEx stia causando il problema. Sinceramente non sono sicuro di dove le cose potrebbero essere ingannate dal momento che tutti i codici di errore che ho intrappolato mostrano il successo. Per riferimento, ecco la firma della funzione per RegCreateKeyEx:
/*
* LONG WINAPI RegCreateKeyEx(
__in HKEY hKey,
__in LPCTSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPTSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
);
*/
Ogni pensiero sarebbe fantastico!
grazie, brian
Dove si verifica esattamente il tuo errore (presumo alla chiamata RegCreateKeyEx(), ma non dovremmo indovinare), e cosa restituisce GetLastError()? –
Hai intenzione di provare a eliminare la chiave appena creata? –
Invece di REG_OPTION_BACKUP_RESTORE dovresti passare REG_OPTION_NON_VOLATILE (= 0). È possibile consultare il comportamento di tali opzioni da MSDN http://msdn.microsoft.com/en-us/library/ms724844(VS.85).aspx – Ismael