QB64.org Forum

Active Forums => Programs => Topic started by: SpriggsySpriggs on August 23, 2021, 02:56:31 PM

Title: Windows Registry Library (BI & BM)
Post by: SpriggsySpriggs 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: 
* WinReg.BI
 
* WinReg.BM


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'
Title: Re: Windows Registry Library (BI & BM)
Post by: FellippeHeitor on August 23, 2021, 04:12:11 PM
And just like BAM, it's among us. Thank you so much for sharing it, Zach.
Title: Re: Windows Registry Library (BI & BM)
Post by: SpriggsySpriggs 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.
 
* WinReg.BM
  <-Updated BM file.
 
* WinReg.BI
Title: Re: Windows Registry Library (BI & BM)
Post by: SpriggsySpriggs on October 05, 2021, 10:34:41 PM
Updated the code in the original post. The BI file now has a condition to check that the IDE is at least dev build 2.0 as builds from that point on would be guaranteed to have the CONST bug fixed. The BM file has been updated to have the CONST declarations fixed. ShellExecute has been moved from the CustomType declaration to a Dynamic declaration so as to not cause compilation errors when not running in $CONSOLE:ONLY.