October 30, 2025, 05:20:00 PM

News:

IWBasic runs in Windows 11!


More Registry commands with this new Registry.inc file

Started by Andy, February 07, 2011, 06:38:21 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi everyone,

I have been working on the registry.inc file again, I have now updated it to include enumerating keys, sub keys, and registry values.

This is a test program with examples of how to use the registry.inc file and the new Enum commands - hope this is of use to all, any questions please reply to this post and I will try and help.

The new registry.inc file is after the progam.


$INCLUDE "Registry.inc"

DEF oldretval:string
DEF examkey:string
DEF values:INT
DEF getval:string
Def result as INT
Def resultin as string

'================================================================================================================
'
'Extended registry commands - please enjoy - Andrew.
'
'Def result as INT
'Def resultin as string
'Two return values I use with these registry commands
'
'result = RegCreateKey("HKEY_CURRENT_USER\Software\KMS")    - Creates a registry key
'
'result = RegCreateKey("HKEY_CURRENT_USER\Software\KMS\OT") - Creates a registry sub key
'                 - Creates both key and sub key if key does not exist
'
'result = RegDeleteKey("HKEY_CURRENT_USER\Software\KMS")    - Deletes a registry key
'                 - Deletes a registry key AND sub keys - be careful!
'
'result = RegDeleteKey("HKEY_CURRENT_USER\Software\KMS\OT") - Deletes a registry sub key
'
'resultin = RegDeleteValue("HKEY_CURRENT_USER\Software\KMS", "RP")
'                - Deletes a registry entry called "RP"
'
'result = RegGetValue - Gives you the value of the entry
'
'result = RegSetValue("HKEY_CURRENT_USER\Software\KMS", "something", "tutorial")
'                - Creates a string entry called "tutorial"
'                - Sets it's value to "something"
'
'result = RegSetDWValue("HKEY_CURRENT_USER\Software\KMS", 1, "test")
'                - Creates a Dword entry called "test"
'                - Sets value to 1
'
'
'The following commands are exampled below in this program:
'
'RegCountSubKeys - counts the number of sub keys for "HKEY_CURRENT_USER\\Software\\KMS"
'
'RegEnumKey      - lists a sub key name of ("HKEY_CURRENT_USER\\Software\\KMS",0)
'                - you must count from zero, the next sub key is 1, the 2 etc
'
'RegCountEntries - Counts the number of registry entries for "HKEY_CURRENT_USER\\Software\\KMS"
'
'RegEnumEntryName- lists the name of an registry entry at the specified key "HKEY_CURRENT_USER\\Software\\KMS"
'                - you must count from zero, the next entry is 1, then 2 etc
'       
'RegGetEntryType - Tells you if it's a String or Dword entry
'
'================================================================================================================


'Key to be enumerated - CHANGE THIS TO A VALID REGISTRY KEY OF YOUR CHOICE
examkey = "HKEY_CURRENT_USER\\Software\\KMS"

OPENCONSOLE

PRINT
PRINT "Key to enumerated = ",examkey
PRINT

'Count the number of sub keys
ret = RegCountSubKeys(examkey)
PRINT "Number of sub-keys = ", ret
PRINT

'Enumerate sub keys
FOR y = 0 to (ret-1)
retval = RegEnumKey(examkey,y)
PRINT "Sub-key ",(y+1), retval
NEXT y

PRINT
PRINT "Press any key to list registry entries for ",examkey
DO:UNTIL INKEY$ <> ""
PRINT CLS

'Count the number of values (entries) for the key "HKEY_CURRENT_USER\\Software\\KMS"
PRINT
values = RegCountEntries(examkey)
PRINT values,"entries for ",examkey
PRINT

'Enumerate the entry names
FOR y = 0 to (values-1)
retval = RegEnumEntryName(examkey,y)
PRINT examkey," entry ",(y+1), retval

'Display the type - string,dword etc
showtype = RegGetEntryType(examkey,retval)

IF showtype = 1
   PRINT "The entry '",retval,"' is a String"
ENDIF

IF showtype = 4
   PRINT "The entry '",retval,"' is a Dword"
ENDIF

'Display the stored value of the entry
resultin = RegGetValue(examkey,retval)
PRINT "Value is ",resultin
PRINT
NEXT y

'Finish
PRINT "Press any key to finish."
DO:UNTIL INKEY$ <> ""
END

---------------------------- This is the new registry.inc file ------------------------------------------------------------------------------------------

TYPE FileTime
   INT dwLowDateTime
   INT dwHighDateTime
ENDTYPE

DECLARE IMPORT,RegOpenKeyExA(hkey  as Uint,subkey as string,options  as int,security  as int,result as pointer),INT
DECLARE IMPORT,RegQueryValueExA(hkey as Uint,valuename as string,reserved  as pointer,typ as pointer,keyvalue as pointer,size as pointer),INT
DECLARE IMPORT,RegCloseKey(hkey  as Uint),INT
DECLARE IMPORT,RegCreateKeyExA(hKey AS INT,lpSubKey AS STRING,Reserved AS INT,lpClass AS STRING,dwOptions AS INT,samDesired AS INT,lpSecurityAttributes AS int,phkResult AS INT BYREF,lpdwDisposition AS INT BYREF),INT
DECLARE IMPORT,RegDeleteKeyA(hKey AS Uint,SubKey AS STRING),INT
DECLARE IMPORT,RegSetValueExA(hKey AS INT,lpValueName AS STRING,Reserved AS INT,dwType AS INT,lpData AS POINTER,cbData AS INT),INT
DECLARE IMPORT,RegDeleteValueA(hKey:INT, lpValueName:STRING),INT 
DECLARE IMPORT,RegEnumKeyExA(hKey AS INT,dwIndex AS INT,lpName AS STRING,lpcbName AS POINTER,lpReserved AS INT,lpClass AS STRING,lpcbClass AS POINTER,lpftLastWriteTime AS FILETIME),INT
DECLARE IMPORT,RegEnumValueA(hKey AS INT,dwIndex AS INT,lpValueName AS STRING,lpcbValueName AS POINTER,lpReserved AS INT,lpType AS POINTER,lpData AS POINTER BYREF,lpcbData AS POINTER),INT
DECLARE IMPORT,RegQueryInfoKeyA(INT hKey,POINTER lpClass,POINTER lpcchClass,POINTER lpReserved,POINTER lpcSubKeys,POINTER lpcbMaxSubKeyLen,POINTER lpcbMaxClassLen,POINTER lpcValues,POINTER lpcbMaxValueNameLen,POINTER lpcbMaxValueLen,POINTER lpcbSecurityDescriptor,FILETIME lpftLastWriteTime),INT

SETID "HKEY_CLASSES_ROOT", 0x80000000
SETID "HKEY_CURRENT_USER", 0x80000001
SETID "HKEY_LOCAL_MACHINE", 0x80000002
SETID "HKEY_USERS", 0x80000003
SETID "HKEY_PERFORMANCE_DATA",0x80000004
SETID "HKEY_CURRENT_CONFIG", 0x80000005
SETID "HKEY_DYN_DATA", 0x80000006
SETID "KEY_ALL_ACCESS", 63
SETID "REG_SZ",1
SETID "REG_BINARY", 3
SETID "REG_DWORD", 4   

Sub GetRoot(Key as string),UInt
   string rf
   UInt retval
   retval=0
   rf=left$(Key,instr(Key,"\\")-1)
   select rf
      case "HKEY_CLASSES_ROOT"
         retval= @HKEY_CLASSES_ROOT
      case "HKEY_CURRENT_USER"
         retval=@HKEY_CURRENT_USER
      case "HKEY_LOCAL_MACHINE"
         retval=@HKEY_LOCAL_MACHINE
      case "HKEY_USERS"
         retval=@HKEY_USERS
      case "HKEY_PERFORMANCE_DATA"
         retval=@HKEY_PERFORMANCE_DATA
      case "HKEY_CURRENT_CONFIG"
         retval=@HKEY_CURRENT_CONFIG
      case "HKEY_DYN_DATA"
         retval=@HKEY_DYN_DATA
   endselect
   return retval
EndSub

Sub RegGetValue(Key as string,opt ValueName as string),string
   string retval,rf,ky
   UInt root,ret,ln,typ
   int getdw
   retval=""
   ln=255
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0
      RegQueryValueExA(Ret,ValueName,NULL,typ,retval,ln)
      if typ=4:'Dword
         ln=4
         RegQueryValueExA(Ret,ValueName,NULL,typ,getdw,ln)
         retval=str$(getdw)
      endif
   endif
   RegCloseKey(ret)
   return retval
EndSub

Sub RegGetEntryType(Key as string,opt ValueName as string),INT
   string retval,rf,ky
   UInt root,ret,ln,typ
   int getdw
   retval=""
   ln=255
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0
      RegQueryValueExA(Ret,ValueName,NULL,typ,retval,ln)
   endif
   RegCloseKey(ret)
   return typ
EndSub

Sub RegDeleteValue(Key as string,opt ValueName as string),string

   string retval,rf,ky
   UInt root,ret,ln,typ
   int getdw
   retval=""
   ln=255
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0

   IF RegDeleteValueA(ret,valuename) = 0
       retval = "0"
    ELSE
       retval = "1"
    ENDIF

   endif
   RegCloseKey(ret)
   return retval
EndSub

Sub RegSetValue(Key as string,Value as string,opt ValueName as string),int
   string rf,ky
   UInt root,ret,ln,typ
   int retval,tmp
   retval=0
   tmp=0
   ln=255
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   RegCreateKeyExA(GetRoot(Key),ky,0,"",0,@KEY_ALL_ACCESS,0,ret,tmp)
   tmp=0
   ln=len(value)+1
   retval=RegSetValueExA(ret,ValueName,tmp,@REG_SZ,Value,ln)
   RegCloseKey(ret)
   return retval
EndSub

Sub RegSetDWValue(Key as string,Value as int,opt ValueName as string),int
   string rf,ky
   UInt root,ret,ln,typ
   int retval,tmp
   retval=0
   tmp=0
   ln=255
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   RegCreateKeyExA(GetRoot(Key),ky,0,"",0,@KEY_ALL_ACCESS,0,ret,tmp)
   tmp=0
   ln=4
   retval=RegSetValueExA(ret,ValueName,tmp,@REG_DWORD,Value,ln)
   RegCloseKey(ret)
   return retval
EndSub

Sub RegCreateKey(Key as string),int
   string rf,ky
   UInt root,ret
   int retval,tmp,ln
   tmp=0
   ln=255
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   retval=RegCreateKeyExA(GetRoot(Key),ky,0,"",0,@KEY_ALL_ACCESS,0,ret,tmp)
   RegCloseKey(ret)
   return retval
EndSub

Sub RegDeleteKey(Key as string),int
   string rf,ky
   int retval
   rf=left$(Key,instr(Key,"\\")-1)
   ky=right$(Key,len(Key)-1-len(rf))
   retval=RegDeleteKeyA(GetRoot(Key),ky)
   return retval
EndSub

Sub RegEnumKey(STRING Key,INT Index),STRING
    FileTime ft
    UInt root,ret,ln
    INT x
    string rf,ky
   string retval,ln$
   ln=255
   rf=left$(Key,INSTR(Key,"\\")-1)
   ky=right$(Key,(len(Key)-1)-len(rf))
    if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0
       RegEnumKeyExA(ret,index,retval,ln,NULL,"",ln,ft)
    ENDIF
   RegCloseKey(ret)
   return retval
EndSub

Sub RegEnumEntryName(STRING Key,INT Index),string
   FileTime ft
    int getdw
   UINT ret,typ
   STRING retval
    string rf,ky
   INT ln
   lpClass=""
    ln=255
   rf=left$(Key,INSTR(Key,"\\")-1)
   ky=right$(Key,(len(Key)-1)-len(rf))
   if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0
       RegEnumValueA(ret,Index,retval,ln,NULL,typ,NULL,NULL)
    ENDIF
   RegCloseKey(ret)
   return retval
EndSub

Sub RegCountSubKeys(STRING Key),INT
   FileTime ft
   INT ln,retval
    ln=255
   UINT ret
   lpClass=""
   rf=left$(Key,INSTR(Key,"\\")-1)
   ky=right$(Key,(len(Key)-1)-len(rf))
   if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0
      RegQueryInfoKeyA(ret,lpClass,ln,NULL,retval,NULL,NULL,NULL,NULL,NULL,NULL,ft)
   RegCloseKey(ret)
    ENDIF
   'Returns the number of sub-keys under this key, or 0 for error
   return retval
EndSub

Sub RegCountEntries(STRING Key),INT
   FileTime ft
   UINT ret
   INT ln
   INT retval
   STRING lpClass
    string rf,ky
    lpClass = ""
    ln=255
   rf=left$(Key,INSTR(Key,"\\")-1)
   ky=right$(Key,(len(Key)-1)-len(rf))
   if RegOpenKeyExA(GetRoot(Key),ky,0,@KEY_ALL_ACCESS,ret) = 0
      RegQueryInfoKeyA(ret,lpClass,ln,NULL,NULL,NULL,NULL,retval,NULL,NULL,NULL,ft)
   RegCloseKey(ret)
    ENDIF
   'Returns the number of values under this key
   return retval
EndSub

---------------------------------------------------------------------------------------------------------------------------------------

Hope this helps you,
Best wishes,
Andrew.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.