RegQueryInfoKey不提供注册表项名称文本



使用RegQueryInfoKey,如在http://msdn.microsoft.com/en-us/library/windows/desktop/ms724256(v=vs.85).aspx上提供的示例中,没有在输出参数achClass中给我注册表文件夹名称。我总是收到一个空字符串。我的问题是:我是否必须调用任何其他函数后获得密钥名称的文本?或者我可以用这个,但我遗漏了什么?

旁注:本机为Windows 7, 64位。

Thanks in advance

更新:代码

//调用站点:

HKEY hKey;
LSTATUS status= RegOpenKeyEx(HKEY_CURRENT_USER, InstanceFullName, 0, KEY_ALL_ACCESS, &hKey);
if ( status != ERROR_SUCCESS)
{
    LPVOID lpMsgBuf;
    FormatMessage( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        status,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL 
    );
    this->MessageBox( (LPCTSTR)lpMsgBuf, GetProgramTitle(), MB_OK | MB_ICONERROR );
    LocalFree( lpMsgBuf );
    return;     
}
status= MyRegSaveKey(hKey, sTempRegFilePath, NULL);

//

调用函数
LSTATUS MyRegSaveKey(_In_ HKEY hKey, _In_ LPCTSTR lpFile, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
CXmlWriter xmlWriter;
if(!xmlWriter.Begin(lpFile))
    return ERROR_CANNOT_MAKE;
LSTATUS retCode= _MyRegSaveKey(hKey, lpFile, lpSecurityAttributes, xmlWriter);
xmlWriter.Finish();
return retCode;
}

//内部调用函数

LSTATUS _MyRegSaveKey(_In_ HKEY hKey, _In_ LPCTSTR lpFile, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, CXmlWriter& writer)
{
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms724256(v=vs.85).aspx
TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
DWORD    cbName;                   // size of name string 
TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
achClass[0] = ''; 
DWORD    cchClassName = MAX_PATH;  // size of class string 
DWORD    cSubKeys=0;               // number of subkeys 
DWORD    cbMaxSubKey;              // longest subkey size 
DWORD    cchMaxClass;              // longest class string 
DWORD    cValues;              // number of values for key 
DWORD    cchMaxValue;          // longest value name 
DWORD    cbMaxValueData;       // longest value data 
DWORD    cbSecurityDescriptor; // size of security descriptor 
FILETIME ftLastWriteTime;      // last write time 
DWORD i, retCode; 
TCHAR  achValue[MAX_VALUE_NAME]; 
DWORD cchValue = MAX_VALUE_NAME;
DWORD type;
BYTE* pData= NULL;
DWORD size;
// Get the class name and the value count. 
retCode = RegQueryInfoKey(
    hKey,                    // key handle 
    achClass,                // buffer for class name 
    &cchClassName,           // size of class string 
    NULL,                    // reserved 
    &cSubKeys,               // number of subkeys 
    &cbMaxSubKey,            // longest subkey size 
    &cchMaxClass,            // longest class string 
    &cValues,                // number of values for this key 
    &cchMaxValue,            // longest value name 
    &cbMaxValueData,         // longest value data 
    &cbSecurityDescriptor,   // security descriptor 
    &ftLastWriteTime       // last write time 
);
if(retCode != ERROR_SUCCESS)
    return retCode;
//... the rest does not make any difference
return ERROR_SUCCESS;
}

现在我可以看到代码了,看起来您希望获得打开的密钥的名称。

你不能使用RegQueryInfoKey()的lpClass参数——参见这个SO答案;看起来您提到的示例只使用RegQueryInfoKey()调用中的cSubKeyscValues

从这个SO答案[即使这个问题是针对Perl的],它看起来不像Win32注册表API有一个函数可以让你接受句柄并返回键名。

在同一篇文章中,只有两个实际的解决方案是

  • 维护从Open返回的对象列表以及到它们的路径

  • 扩展Win32::Registry API以调用ntdll.dll中的NtQueryKey导出函数,并执行此stackoverflow答案

最新更新