Chinaunix首页 | 论坛 | 博客
  • 博客访问: 242964
  • 博文数量: 52
  • 博客积分: 1355
  • 博客等级: 中尉
  • 技术积分: 485
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-06 12:23
文章分类

全部博文(52)

文章存档

2013年(5)

2012年(16)

2011年(26)

2010年(2)

2009年(1)

2008年(2)

我的朋友

分类: C/C++

2012-02-07 15:15:08

#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
*ZwQueryKeyType)(
HANDLE KeyHandle,
int KeyInformationClass,
PVOID KeyInformation,
ULONG Length,
PULONG ResultLength);

ZwQueryKeyType func
= reinterpret_cast<ZwQueryKeyType>(::GetProcAddress(dll, "ZwQueryKey"));

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];
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;
}
阅读(1032) | 评论(0) | 转发(0) |
0

上一篇:常用命令

下一篇:mfc目录选取

给主人留下些什么吧!~~