Search for the currently opened registry path key name based on the hkey in C ++

Source: Internet
Author: User

Stackoverflow must be collected.

# Include < Windows. h >
# Include < String >

Typedef long ntstatus;

# Ifndef STATUS_SUCCESS
# DefineSTATUS_SUCCESS (ntstatus) 0x00000000l)
# Endif

# Ifndef status_buffer_too_small
# DefineStatus_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 );
}
}
ReturnKeypath;
}

Int_ Tmain (IntArgc, _ 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;
}

Output:

Key Path For 00000fdc Is   ' \ REGISTRY \ MACHINE \ Software \ Microsoft ' .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.