Author Topic: Windows Registry Library (BI & BM)  (Read 171 times)

0 Members and 1 Guest are viewing this topic.

Online SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1109
  • If you're API and you know it clap your hands
    • My GitHub
Windows Registry Library (BI & BM)
« on: August 23, 2021, 02:56:31 PM »
I've written a library for the Windows Registry. The code works for 64 bit. I have not considered 32 bit at all. It may or may not work for that architecture.

External libraries used:

winreg.h
shlwapi.dll
shell32.dll
shellapi.h



List of FUNCTIONS/SUBS contained in library:

SelfElevate (This function is used automatically by the library when certain registry keys are accessed.)
CreateRegistryKey (Creates a registry key)
DeleteRegistryKey (WARNING: Recursively deletes a key and its subkeys.)
WriteREG_DWORD (Writes an UNSIGNED LONG value to a key)
WriteREG_QWORD (Writes an UNSIGNED INTEGER64 value to a key)
WriteREG_SZ (Writes a STRING value to a key)
ReadREG_DWORD (Reads an UNSIGNED LONG value from a key)
ReadREG_QWORD (Reads an UNSIGNED INTEGER64 value from a key)
ReadREG_SZ (Reads a STRING value from a key)
IsInstalled (Iterates through the x86 registry to determine if a program is installed. Calls IsInstalled64 if the program is not found.)
IsInstalled64 (Iterates through the x64 registry to determine if a program is installed. Call IsInstalled instead of calling this function directly.)
CheckVersion (Iterates through the x86 registry to locate the specified program and return the version string. Calls CheckVersion64 if the program is not found.)
CheckVersion64 (Iterates through the x64 registry to locate the specified program and return the version string. Call CheckVersion instead of calling this function directly.)
GetUninstallString (Iterates through the x86 registry to locate the specified program and return the uninstall string. Calls GetUninstallString64 if the program is not found.)
GetUninstallString64 (Iterates through the x64 registry to locate the specified program and return the uninstall string. Call GetUninstallString instead of calling this function directly.)
EnumInstalledPrograms (Iterates through the x86 registry to enumerate all installed programs. Calls EnumInstalledPrograms64 so as to completely list all programs.)
EnumInstalledPrograms64 (Iterates through the x64 registry to enumerate all installed programs. Call EnumInstalledPrograms instead of calling this function directly.)


The library:   

Sample code:
Code: QB64: [Select]
  1. '$Include:'WinReg.BI'
  2.  
  3. 'The following code demonstrates creating a new registry key to use a custom URL protocol of "qb64:" to launch qb64.exe
  4. DIM AS Byte status
  5.  
  6. status = CreateRegistryKey(HKEY_CLASSES_ROOT, "qb64")
  7. IF status <> REG_TRUE THEN
  8.     PRINT "Failed to create key"
  9.     END
  10.  
  11. status = WriteREG_SZ(HKEY_CLASSES_ROOT, "qb64", "", "URL:QB64 Protocol")
  12. IF status <> REG_TRUE THEN
  13.     PRINT "Failed to insert value into registry key"
  14.     END
  15.  
  16. status = WriteREG_SZ(HKEY_CLASSES_ROOT, "qb64", "URL Protocol", "")
  17. IF status <> REG_TRUE THEN
  18.     PRINT "Failed to insert value into registry key"
  19.     END
  20.  
  21. status = WriteREG_SZ(HKEY_CLASSES_ROOT, "qb64", "DefaultIcon", "qb64.exe,1")
  22. IF status <> REG_TRUE THEN
  23.     PRINT "Failed to insert value into registy key"
  24.     END
  25.  
  26. status = CreateRegistryKey(HKEY_CLASSES_ROOT, "qb64\shell\open\command")
  27. IF status <> REG_TRUE THEN
  28.     PRINT "Failed to create key"
  29.     END
  30.  
  31. status = WriteREG_SZ(HKEY_CLASSES_ROOT, "qb64\shell\open\command", "", CHR$(34) + _CWD$ + "\qb64.exe" + CHR$(34) + " " + CHR$(34) + "%1" + CHR$(34))
  32. IF status <> REG_TRUE THEN
  33.     PRINT "Failed to insert value into registry key"
  34.     END
  35.  
  36. status = ReadREG_SZ(HKEY_CLASSES_ROOT, "qb64\shell\open\command", "", cmd)
  37. IF status <> REG_TRUE THEN
  38.     PRINT "Failed to read value"
  39.     END
  40.  
  41.  
  42. 'Delete the key we just made
  43.  
  44. PRINT DeleteRegistryKey(HKEY_CLASSES_ROOT, "qb64")
  45. '$Include:'WinReg.BM'

Code: QB64: [Select]
  1.  '$Include:'WinReg.BI'
  2. 'The following code demonstrates checking if PowerShell 7 is installed and, if so, displays the version string and the uninstall string
  3. IF IsInstalled("powershell 7-x64") THEN
  4.     PRINT "Installed"
  5.     PRINT CheckVersion("powershell 7-x64")
  6.     PRINT GetUninstallString("powershell 7-x64")
  7.  
  8. EnumInstalledPrograms
  9. '$Include:'WinReg.BM'
« Last Edit: August 23, 2021, 02:59:41 PM by SpriggsySpriggs »
If you're API and you know it clap your hands
My GitHub

Online FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 3072
  • Let it go, this too shall pass.
    • QB64.org
Re: Windows Registry Library (BI & BM)
« Reply #1 on: August 23, 2021, 04:12:11 PM »
And just like BAM, it's among us. Thank you so much for sharing it, Zach.

Marked as best answer/most recent update by SpriggsySpriggs on August 24, 2021, 02:32:45 AM

Online SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1109
  • If you're API and you know it clap your hands
    • My GitHub
Re: Windows Registry Library (BI & BM)
« Reply #2 on: August 23, 2021, 11:32:39 PM »
Update: For some odd reason, the code wouldn't compile until I moved one of the external functions into a DLL block. Moved it. Should work. Also, tested the code in 32 bit and found that the section for enumerating installed files worked. The rest seems to not. Note: I've still only tested this code on Windows 10 and Windows 11.
   <-Updated BM file.
 
« Last Edit: August 23, 2021, 11:34:59 PM by SpriggsySpriggs »
If you're API and you know it clap your hands
My GitHub