Read/Write Registry (Win)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Read/Write Registry (Win)

Post by vdecampo »

Here are two basic functions for reading and writing to the Windows registry. It can read DWORD, STRING, and BINARY keys. It can write STRING and DWORD keys. Feel free to use, expand, or improve.

Registry Functions

Code: Select all

/'
   Basic Windows Registry Read/Write Functions
   Translated from VB6 source by
      -Vincent DeCampo, 2010
   
   Original author UNKOWN    
'/

#Include "windows.bi"
#Include "vbcompat.bi"

' Possible registry data types
Enum InTypes
   ValNull = 0
   ValString = 1
   ValXString = 2
   ValBinary = 3
   ValDWord = 4
   ValLink = 6
   ValMultiString = 7
   ValResList = 8
End Enum

' Registry section definitions
'Const HKEY_CLASSES_ROOT = &H80000000
'Const HKEY_CURRENT_USER = &H80000001
'Const HKEY_LOCAL_MACHINE = &H80000002
'Const HKEY_USERS = &H80000003
'Const HKEY_PERFORMANCE_DATA = &H80000004
'Const HKEY_CURRENT_CONFIG = &H80000005

Function ReadRegistry(ByVal Group as HKEY, ByVal Section As LPCSTR, ByVal Key As LPCSTR) As String
Dim as DWORD lDataTypeValue, lValueLength
Dim sValue As String * 2048
Dim As String Tstr1, Tstr2  
Dim lKeyValue As HKEY
Dim lResult as Integer
Dim td As Double

   sValue = ""
   
   lResult      = RegOpenKey(Group, Section, @lKeyValue)
   lValueLength = Len(sValue)
   lResult      = RegQueryValueEx(lKeyValue, Key, 0&, @lDataTypeValue, Cast(Byte Ptr,@sValue), @lValueLength)
   
   If (lResult = 0) Then

      Select Case lDataTypeValue
         case REG_DWORD 
            td = Asc(Mid(sValue, 1, 1)) + &H100& * Asc(Mid(sValue, 2, 1)) + &H10000 * Asc(Mid(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid(sValue, 4, 1)))
            sValue = Format(td, "000")
         case REG_BINARY 
            ' Return a binary field as a hex string (2 chars per byte)
            Tstr2 = ""
            For I As Integer = 1 To lValueLength
               Tstr1 = Hex(Asc(Mid(sValue, I, 1)))
               If Len(Tstr1) = 1 Then Tstr1 = "0" & Tstr1
               Tstr2 += Tstr1
            Next
            sValue = Tstr2
         Case Else
            sValue = Left(sValue, lValueLength - 1)
      End Select
   
   End If

   lResult = RegCloseKey(lKeyValue)
   
   Return sValue

End Function

Sub WriteRegistry(ByVal Group as HKEY, ByVal Section As LPCSTR, ByVal Key As LPCSTR, ByVal ValType As InTypes, value As String)
Dim lResult as Integer
Dim lKeyValue As HKEY
Dim lNewVal as DWORD
Dim sNewVal As String * 2048

   lResult = RegCreateKey(Group, Section, @lKeyValue)

   If ValType = ValDWord Then
      lNewVal = CUInt(value)
      lResult = RegSetValueEx(lKeyValue, Key, 0&, ValType, Cast(Byte Ptr,@lNewVal), SizeOf(DWORD))
   Else
      If ValType = ValString Then
         sNewVal = value & Chr(0)
         lResult = RegSetValueEx(lKeyValue, Key, 0&, ValString, Cast(Byte Ptr,@sNewVal), Len(sNewVal))
      EndIf
   End If

   lResult = RegFlushKey(lKeyValue)
   lResult = RegCloseKey(lKeyValue)

End Sub
Example Usage

Code: Select all

Dim regpath As string 
Dim keyname As String 

/'
   Example to read a key
'/

regpath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
keyname = "ProductName"
Print keyname & "=" & ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname)

/'
   Example to write a key
'/
regpath = "SOFTWARE\MySoftwareCompany"
keyname = "MyEntry"

WriteRegistry (HKEY_CURRENT_USER,regpath, keyname, ValDword, "25")

Print "Done! Press Any Key..."
Sleep
-Vince
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

This has a million uses.

Now, I have a permanent spot to tinker with, as well as my sandbox, the %TEMP% folder.



~Kiyote!
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Read/Write Registry (Win)

Post by Roland Chastain »

Hello !

How to get the language settings ?

I tried this but apparently it isn't the good way :

Code: Select all

dim regpath AS STRING
dim keyname AS STRING

regpath = "Control Panel\Desktop"
keyname = "MultiUILanguageId"
print keyname & "=" & ReadRegistry(HKEY_CURRENT_USER, regpath, keyname)
My OS is Windows 8. I tested the example given by vdecampo : it works.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Read/Write Registry (Win)

Post by vdecampo »

Perhaps you want to investigate using the WinAPI for doing this....

http://stackoverflow.com/questions/1322 ... -of-the-os

-Vince
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Read/Write Registry (Win)

Post by Roland Chastain »

vdecampo wrote:Perhaps you want to investigate using the WinAPI for doing this....

http://stackoverflow.com/questions/1322 ... -of-the-os

-Vince
Thank you for the answer and the link. I believed I could do that with your code.

Very useful code, anyway.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Read/Write Registry (Win)

Post by TJF »

@Roland

It seems that you're looking for the users language settings. This may be tricky on win32
GLib documentation wrote:Locale

...

On Windows, in a C program there are several locale concepts that not necessarily are synchronized. On one hand, there is the system default ANSI code-page, which determines what encoding is used for file names handled by the C library's functions and the Win32 API. (We are talking about the "narrow" functions here that take character pointers, not the "wide" ones.)

On the other hand, there is the C library's current locale. The character set (code-page) used by that is not necessarily the same as the system default ANSI code-page. Strings in this character set are returned by functions like strftime().
The most reliable way to handle correct locale settings is to use a toolkit. In GTK+ the function gtk_init() handles that. It also switches to the right translation file(s) when gettext is used for I18N.
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Read/Write Registry (Win)

Post by Roland Chastain »

Thank you, TJF. I will have a look at GTK. Maybe I will also open a new discussion in the beginner forum, about user language settings.
Makoto WATANABE
Posts: 231
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: Read/Write Registry (Win)

Post by Makoto WATANABE »

Dear vdecampo

Thank you for posting programs.
I was introduced your programs to Japanese people.
http://makoto-watanabe.main.jp/freebasi ... #ReadWrite
Please consent to this.
thrive4
Posts: 70
Joined: Jun 25, 2021 15:32

Re: Read/Write Registry (Win)

Post by thrive4 »

@vdecampo
Very nice thank you for sharing the code with us.

My two cents:
Check for x64 and / or x86 vcredist 2015 or later

Code: Select all

Dim regpath As string 
Dim keyname As String 

#ifdef __FB_WIN32__
    regpath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
    keyname = "ProductName"
    Print keyname & "=" & ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname)

    ' todo work out group
    regpath = "SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\x64"
    keyname = "Version"
    'Print keyname & "=" & ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname)
    if ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname) <> "" then
        print "x64 vcredist 2015 or later found version: " & ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname) 
    else 
        print "x64 vcredist 2015 or later not found!"
    end if

    regpath = "SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\x86"
    keyname = "Version"
    'Print keyname & "=" & ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname)
    if ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname) <> "" then
        print "x86 vcredist 2015 or later found version: " & ReadRegistry(HKEY_LOCAL_MACHINE,regpath,keyname)
    else 
        print "x86 vcredist 2015 or later not found!"
    end if

    ' 32 bit
    'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\x86
    'https://github.com/bitbeans/RedistributableChecker/blob/master/RedistributableChecker/RedistributablePackage.cs
#else
    'nop
#endif

sleep
end
Post Reply