我正在尝试获取我在“REG_SZ”类型的注册表项“userpath”上设置的值。代码的简化版本如下。
#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;
}
我在第二个RegQueryValueE winapi之后获得'lpbCheckValue'的垃圾值。请指出我做错了什么?
答案 0 :(得分:0)
尝试使用RegQueryValueExA
和RegSetValueExA
。如果您的项目是unicode,Visual Studio会自动使用RegQueryValueExW
和RegSetValueExW
,其中预期值基于wchar_t
。
答案 1 :(得分:0)
sizeof(sVal)
给出了什么? 4?我不确定你为什么使用wcslen而不是strlen,因为sVal是一个char *,而不是wchar_t *。