WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Registry UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Registry Path From Registry Handle


Question:

Is there a way of getting the path of a registry handle? In other words if XX is a handle to the registry key "HKEY_LOCAL_MACHINE\SOFTWARE", is there a way of getting a string representing the path to the key by only having the handle?

Answer:

Here's a snippet of sample code that uses a UDF that I wrote that converts a registry key handle back into a registry key path name.

Please note that only local registry key handles can be converted into registry key path names; remote key handles aren't supported.

Also, this only works on the WinNT platform family; the Win9x platform family is not supported.

Finally, the registry key path name will have a prefix indicating which registry hive the key located in, so you may have to remove a prefix similar to "\REGISTRY\MACHINE\" in order to get just the key name if the key is located under HKEY_LOCAL_MACHINE [e.g. was opened using @REGMACHINE with RegOpenKey()].

; GetRegKeyPathFromHKEY.wbt
; 32-bit UDF



#DefineFunction GetRegKeyPathFromHKEY(hKey)

Title01 = 'GetRegKeyPathFromHKEY()'

cnKeyNameInformation = 3
cnKNIBaseSize = 4

cnSTATUS_SUCCESS = 0
cnSTATUS_INVALID_HANDLE = 3221225480
cnSTATUS_INVALID_PARAMETER = 3221225485
cnSTATUS_BUFFER_TOO_SMALL = 3221225507


; Validate our input parameters

if (hKey & 1)
  TempMsg = StrCat(Title01,': Error!  hKey is a remote key handle.')
  IntControl(79,-1,7000,TempMsg,0)
endif

if (hKey == 0)
  TempMsg = StrCat(Title01,': Error!  hKey is NULL.')
  IntControl(79,-1,7000,TempMsg,0)
endif


; Figure out how many bytes of memory ZwQueryKey() needs to store its
; return value in.

hSizeBuf = BinaryAlloc(4)

if (hSizeBuf == 0)
  TempMsg = StrCat(Title01,': Error!  Unable to allocate a binary buffer {hSizeBuf}.')
  IntControl(79,-1,7000,TempMsg,0)
endif

BinaryPoke4(hSizeBuf,0,0)

Result = DllCall('NTDLL.dll',long:'ZwQueryKey',long:hKey,long:cnKeyNameInformation,lpnull,long:0,lpbinary:hSizeBuf)

if (Result != cnSTATUS_BUFFER_TOO_SMALL)
  hSizeBuf = BinaryFree(hSizeBuf)
  TempMsg = StrCat(Title01,': Error!  ZwQueryKey() {#1} returned ',Result,'.')
  IntControl(79,-1,7000,TempMsg,0)
endif

nSizeBuf = BinaryPeek4(hSizeBuf,0) + cnKNIBaseSize

hKNIBuf = BinaryAlloc(nSizeBuf)

if (hKNIBuf == 0)
  hSizeBuf = BinaryFree(hSizeBuf)
  TempMsg = StrCat(Title01,': Error!  Unable to allocate a binary buffer {hKNIBuf}.')
  IntControl(79,-1,7000,TempMsg,0)
endif

Result = DllCall('NTDLL.dll',long:'ZwQueryKey',long:hKey,long:cnKeyNameInformation,lpbinary:hKNIBuf,long:nSizeBuf,lpbinary:hSizeBuf)

if (Result != cnSTATUS_SUCCESS)
  hSizeBuf = BinaryFree(hSizeBuf)
  hKNIBuf = BinaryFree(hKNIBuf)
  TempMsg = StrCat(Title01,': Error!  ZwQueryKey() {#2} returned ',Result,'.')
  IntControl(79,-1,7000,TempMsg,0)
endif

BinaryEodSet(hKNIBuf,nSizeBuf)
nSizeResult = BinaryPeek4(hSizeBuf,0)
hSizeBuf = BinaryFree(hSizeBuf)

Result = BinaryConvert(hKNIBuf,3,1,1,0)

sKeyName = BinaryPeekStr(hKNIBuf,2,Result)

return sKeyName

#EndFunction

hMyKey = RegOpenKey(@REGMACHINE,'Software')

sMyKey = GetRegKeyPathFromHKEY(hMyKey)

exit



Article ID:   W16741
File Created: 2005:02:18:12:22:10
Last Updated: 2005:02:18:12:22:10