首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从C++中的HKEY句柄确定注册表项的路径

从C++中的HKEY句柄确定注册表项的路径
EN

Stack Overflow用户
提问于 2009-06-01 22:06:14
回答 4查看 17.8K关注 0票数 20

给定Windows注册表项的句柄,比如由::RegOpenKeyEx()设置的注册表项,是否可以确定该注册表项的完整路径?

我意识到,在一个简单的应用程序中,你所要做的就是查找5到10行代码并阅读……但在像我正在调试的这款复杂的应用程序中,我感兴趣的密钥可以通过一系列调用打开。

EN

回答 4

Stack Overflow用户

发布于 2009-06-01 22:19:56

名义上没有,因为它只是一个句柄,而且据我所知,没有任何API可以让你在普通的Windows API中做到这一点。

然而,Native API有很多函数,其中一些函数可以为您提供针对给定文件打开的句柄,等等,因此注册表可能有类似的东西。That和SysInternals的RegMon可能会做这样的事情,但恐怕你得用谷歌搜索:/

票数 1
EN

Stack Overflow用户

发布于 2009-06-01 22:40:32

您可以使用RegSaveKey并将其写入一个文件,然后查看该文件。

或者,您可以保留HKEY到LPCWSTR的全局映射,并在打开它们时添加条目,并随时进行查找。

您也可以在WinDBG / NTSD中使用!reg命令执行某些操作,但您不能只给它指定HKEY。你将不得不做一些其他的花招来从其中获得你想要的信息。

票数 1
EN

Stack Overflow用户

发布于 2014-12-16 09:48:58

我很兴奋地找到了这篇文章和它喜欢的解决方案。直到我发现我的系统的NTDLL.DLL没有NtQueryKeyType。

在四处寻找之后,我在DDK论坛上偶然发现了ZwQueryKey。

它是用C#编写的,但以下是适合我的解决方案:

enum KEY_INFORMATION_CLASS
{
    KeyBasicInformation,            // A KEY_BASIC_INFORMATION structure is supplied.
    KeyNodeInformation,             // A KEY_NODE_INFORMATION structure is supplied.
    KeyFullInformation,             // A KEY_FULL_INFORMATION structure is supplied.
    KeyNameInformation,             // A KEY_NAME_INFORMATION structure is supplied.
    KeyCachedInformation,           // A KEY_CACHED_INFORMATION structure is supplied.
    KeyFlagsInformation,            // Reserved for system use.
    KeyVirtualizationInformation,   // A KEY_VIRTUALIZATION_INFORMATION structure is supplied.
    KeyHandleTagsInformation,       // Reserved for system use.
    MaxKeyInfoClass                 // The maximum value in this enumeration type.
}
[StructLayout(LayoutKind.Sequential)]
public struct KEY_NAME_INFORMATION
{
    public UInt32 NameLength;     // The size, in bytes, of the key name string in the Name array.
    public char[] Name;           // An array of wide characters that contains the name of the key.
                                  // This character string is not null-terminated.
                                  // Only the first element in this array is included in the
                                  //    KEY_NAME_INFORMATION structure definition.
                                  //    The storage for the remaining elements in the array immediately
                                  //    follows this element.
}

[DllImport("ntdll.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int ZwQueryKey(IntPtr hKey, KEY_INFORMATION_CLASS KeyInformationClass, IntPtr lpKeyInformation, int Length, out int ResultLength);

public static String GetHKeyName(IntPtr hKey)
{
    String result = String.Empty;
    IntPtr pKNI = IntPtr.Zero;

    int needed = 0;
    int status = ZwQueryKey(hKey, KEY_INFORMATION_CLASS.KeyNameInformation, IntPtr.Zero, 0, out needed);
    if ((UInt32)status == 0xC0000023)   // STATUS_BUFFER_TOO_SMALL
    {
        pKNI = Marshal.AllocHGlobal(sizeof(UInt32) + needed + 4 /*paranoia*/);
        status = ZwQueryKey(hKey, KEY_INFORMATION_CLASS.KeyNameInformation, pKNI, needed, out needed);
        if (status == 0)    // STATUS_SUCCESS
        {
            char[] bytes = new char[2 + needed + 2];
            Marshal.Copy(pKNI, bytes, 0, needed);
            // startIndex == 2  skips the NameLength field of the structure (2 chars == 4 bytes)
            // needed/2         reduces value from bytes to chars
            //  needed/2 - 2    reduces length to not include the NameLength
            result = new String(bytes, 2, (needed/2)-2);
        }
    }
    Marshal.FreeHGlobal(pKNI);
    return result;
}

我只在以管理员身份运行时尝试过它,这可能是必需的。

结果的格式有点奇怪:例如,用\REGISTRY\MACHINE\SOFTWARE\company\product代替了HKEY_LOCAL_MACHINE\SOFTWARE\company\product

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/937044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档