Я пытаюсь получить значение, которое я установил в разделе реестра «userpath» типа «REG_SZ».Упрощенная версия кода приведена ниже.
#include <stdio.h>
#include <windows.h>
#include <iostream>
const static char* SrvKey= "System\\CurrentControlSet\\Services\\LanmanServer\\Parameters";
const static char* sVal = "I am probably \a good boy\0";
const static char* keyName = "userpath";
using namespace std;
int main(int argc, char* argv[]){
HKEY hkey;
LONG status;
DWORD wstatus;
DWORD dwCtype;
DWORD dwClen;
DWORD dwSetStatus;
status = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
SrvKey,
(DWORD) 0,
NULL,
REG_OPTION_NON_VOLATILE,
(KEY_READ | KEY_WRITE),
NULL,
&hkey,
NULL
);
if(status != ERROR_SUCCESS){
printf("Error [%d] on creating key handle\n", status);
return status;
}
//RegFlushKey(hkey);
dwClen = strlen(sVal);
dwCtype = REG_SZ;
dwSetStatus = RegSetValueEx(hkey,
keyName,
0,
dwCtype,
(BYTE*)sVal,
dwClen);
if(dwSetStatus != 0){
printf("\nError in setting value in registry. Error [%d]", dwSetStatus);
return dwSetStatus;
}
RegFlushKey(hkey);
DWORD dwCheckType = 0;
DWORD dwCheckLen = 0;
DWORD dwStatus;
dwStatus = RegQueryValueEx( hkey,
keyName,
NULL,
&dwCheckType,
NULL,
&dwCheckLen);
if(dwStatus != 0){
printf("\nError in queering registry for length and type. Error [%d]", dwStatus);
return dwStatus;
}
LPBYTE lpbCheckValue = (LPBYTE) malloc(dwCheckLen);
//dwStatus not check at the moment.
dwStatus = RegQueryValueEx( hkey,
keyName,
NULL,
&dwCheckType,
lpbCheckValue,
&dwCheckLen);
if(dwStatus != 0){
printf("\nError in queering registry for length and type. Error [%d]", dwStatus);
return dwStatus;
}
printf("queried calue is : [%s]", lpbCheckValue);
printf("\n\nEndofProgram\n");
system("PAUSE");
return 0;
}
Я получаю нежелательное значение для 'lpbCheckValue' после второго winQap RegQueryValueE.Пожалуйста, укажите мне, что я делаю не так?