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 ' .