我正在尝试打开 Windows 默认用户的注册表配置单元。我收到错误“参数无效”。我的代码如下:
PHKEY loadDefaultHiveAppKey(){
PHKEY temporaryHKEY = 0;
wchar_t * errorText = 0;
//wchar_t * defaultProfileHiveFile = getDefaultUserProfileHive();
/* For debugging purpouses use a hardcoded path */
wchar_t * defaultProfileHiveFile = L"C:\\Users\\Default\\NTUSER.dat";
long returnCode = 0;
returnCode = RegLoadAppKey(
defaultProfileHiveFile,
temporaryHKEY,
KEY_ALL_ACCESS,
REG_PROCESS_APPKEY,
0
);
//free(defaultProfileHiveFile);
if(returnCode != ERROR_SUCCESS){
// http://stackoverflow.com/questions/455434/how-should-i-use-formatmessage-properly-in-c
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
returnCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&errorText,
0,
NULL
);
printf("Failed to open registry hive!\n");
if(errorText != 0){
/* This prints "The Parameter is Incorrect" */
printf("%ls\n", errorText);
LocalFree(errorText);
errorText = NULL;
} else {
printf("Unknown reason!\n");
}
return 0;
}
return temporaryHKEY;
}
我的 main 基本上只是对前面方法的调用。这是 msdn 的文章RegLoadAppKey
。