20

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

我意识到,在一个简单的应用程序中,您所要做的就是查找 5 或 10 行并阅读......但在像我正在调试的应用程序这样的复杂应用程序中,我感兴趣的密钥可以从一系列中打开的电话。

4

5 回答 5

31

使用LoadLibraryNtQueryKey导出函数,如以下代码片段所示。

#include <windows.h>
#include <string>

typedef LONG NTSTATUS;

#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif

#ifndef STATUS_BUFFER_TOO_SMALL
#define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L)
#endif

std::wstring GetKeyPathFromKKEY(HKEY key)
{
    std::wstring keyPath;
    if (key != NULL)
    {
        HMODULE dll = LoadLibrary(L"ntdll.dll");
        if (dll != NULL) {
            typedef DWORD (__stdcall *NtQueryKeyType)(
                HANDLE  KeyHandle,
                int KeyInformationClass,
                PVOID  KeyInformation,
                ULONG  Length,
                PULONG  ResultLength);

            NtQueryKeyType func = reinterpret_cast<NtQueryKeyType>(::GetProcAddress(dll, "NtQueryKey"));

            if (func != NULL) {
                DWORD size = 0;
                DWORD result = 0;
                result = func(key, 3, 0, 0, &size);
                if (result == STATUS_BUFFER_TOO_SMALL)
                {
                    size = size + 2;
                    wchar_t* buffer = new (std::nothrow) wchar_t[size/sizeof(wchar_t)]; // size is in bytes
                    if (buffer != NULL)
                    {
                        result = func(key, 3, buffer, size, &size);
                        if (result == STATUS_SUCCESS)
                        {
                            buffer[size / sizeof(wchar_t)] = L'\0';
                            keyPath = std::wstring(buffer + 2);
                        }

                        delete[] buffer;
                    }
                }
            }

            FreeLibrary(dll);
        }
    }
    return keyPath;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HKEY key = NULL;
    LONG ret = ERROR_SUCCESS;

    ret = RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft", &key);
    if (ret == ERROR_SUCCESS)
    {
        wprintf_s(L"Key path for %p is '%s'.", key, GetKeyPathFromKKEY(key).c_str());    
        RegCloseKey(key);
    }

    return 0;
}

这将在控制台上打印密钥路径:

00000FDC 的密钥路径是 '\REGISTRY\MACHINE\SOFTWARE\Microsoft'。

于 2009-06-02T00:09:29.017 回答
1

名义上没有,因为它只是一个句柄,而且我知道没有 API 可以让您在普通的 Windows API 中执行此操作。

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

于 2009-06-01T22:19:55.947 回答
1

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

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

您也可以使用 WinDBG / NTSD 中的 !reg 命令做一些事情,但您不能只给它 HKEY。你必须做一些其他的诡计才能得到你想要的信息。

于 2009-06-01T22:40:32.380 回答
1

我很高兴找到这篇文章及其广受欢迎的解决方案。直到我发现我系统的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.

于 2014-12-16T01:48:57.563 回答
0

对于ntsd/windbg

!handle yourhandle 4

于 2010-02-05T01:08:45.050 回答