April 25, 2024, 07:31:06 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Getting a handle of a root key

Started by Andy, September 17, 2017, 04:05:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

September 17, 2017, 04:05:55 AM Last Edit: September 17, 2017, 05:17:36 AM by Andy
Until now, I've never tried to get entries for a root key.

A root key in the registry can be one of the following:

HKEY_CLASSES_ROOTt
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
HKEY_CURRENT_CONFIG

I've only concentrated on sub-keys of the above such as:

HKEY_CURRENT_USER\Software

So how do I get a handle to say HKEY_CURRENT_USER?

This is what microsoft says....
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724897(v=vs.85).aspx

lpSubKey needs to point to HKEY_CURRENT_USER.

This is the code that works for everything else (e.g. sub keys of HKEY_CURRENT_USER)

global Sub RegGetValue(Key as string,opt ValueName as string),string
    string rf,ky
    UInt ret,ln,typ
    uint HexVal[254]
    RegSuccess = 0

    RegBinVal=""
    retval=""
    ln=5000
    RegBinValL = 0
    RegBinVal = ""
    RegStringValue = ""

    rf=left$(Key,instr(Key,"\\")-1)
    ky=right$(Key,len(Key)-1-len(rf))
    if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_READ,ret) = 0
       RegQueryValueExA(Ret,ValueName,NULL,typ,RegStringValue,ln)

       if typ = 0 or typ = 3 or typ = 7:'Bin values
          int xb
          for xb = 0 to 10000
              RegBinVal[xb] = ""
          next xb
          RegQueryValueExA(Ret,ValueName,NULL,typ,RegBinVal,ln)
          RegBinValL = ln-1
          RegCloseKey(ret)
          return RegBinVal
       endif
 
       if typ=4 or typ=11:'Dword - Qword
          RegQueryValueExA(Ret,ValueName,NULL,typ,HexVal,ln)
          return ltrim$(str$(HexVal))
       endif
    endif
    RegCloseKey(ret)
    return RegStringValue
EndSub


The GetRoot(Key) gets the handle to the root key e.g. HKEY_CURRENT_USER, but how do I change the sub key "ky" variable to do as ms says.

By that I mean the line:

if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_READ,ret) = 0

ret stores the required handle.


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

September 17, 2017, 05:18:39 PM #1 Last Edit: September 17, 2017, 05:28:52 PM by fasecero
If I'm understanding correctly that MSDN page, hKey need to be one of the predefined keys, e.g., HKEY_CURRENT_USER, and lpSubKey must be NULL.

Edit. I mean: RegOpenKeyEx(HKEY_CURRENT_USER, 0, ...

Andy

That's how I read it too.

After much playing around I thought perhaps the problem might not be the null ("") parameter, but the root key setting.

Instead of using:

RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_READ,ret)

I substituted GetRoot(Key) with @HKEY_CURRENT_USER like this:

RegOpenKeyExA(@HKEY_CURRENT_USER,"",0,@KEY_READ,ret)

Where @HKEY_CURRENT_USER has been set earlier in the code as:

SETID "HKEY_CURRENT_USER", 0x80000001

This works now!
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.