Reading form the registry (binary code)

Posted on 1998-12-16
Last Modified: 2012-05-04
There's a problem when reading form the system registry.
RegQueryValueEx API does not seem to be working with
the non-string values (for example with
ActiveWindowFontName) or i'm not able to read (convert)
the querried value into the String. Please help.
Question by:ABE2276
  • 4
  • 2
LVL 12

Expert Comment

ID: 1971183
I would like to help, but I can't seem to find ActiveWindowFontName in my registry. Neither could I find any information about it on Microsofts development CDs. Could you tell me where I may find it and what the typical value you want returned looks like ?

Author Comment

ID: 1971184
You've to look for
HKEY_CURRENT_USER\Control Panel\desktop\WindowMetrics\CaptionFont
in your registry.

Thanks for answering :)
LVL 12

Expert Comment

ID: 1971185
Try this:
Copy and paste into a module. Note that you might get duplicate declaration since you already have done some API programming. The function can be expanded to include other parameters, but this is what I had the time to do for now.

From the debug window in my app.

print getwindowssystemfont("Caption","Name")
MS Sans Serif

    Global Const HKEY_CLASSES_ROOT = &H80000000
    Global Const HKEY_CURRENT_USER = &H80000001
    Global Const HKEY_LOCAL_MACHINE = &H80000002
    Global Const HKEY_USERS = &H80000003
    Global Const HKEY_CURRENT_CONFIG As Long = &H80000005

    Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
        "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
        ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
        Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
        Alias "RegQueryValueExA" _
        (ByVal hKey As Long, ByVal lpValueName As String, _
        ByVal lpReserved As Long, lpType As Long, lpData As Any, _
        lpcbData As Long) As Long
    Declare Function RegCloseKey Lib "advapi32.dll" _
        (ByVal hKey As Long) As Long

Function GetWindowsSystemFont(FontType As String, Parameter As String) As String

    Dim temp As String
    Dim Ind As Integer

    Select Case FontType
        Case "Caption"
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "CaptionFont")
        Case "Icon"
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "IconFont")
        Case "Menu"
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "MenuFont")
        Case "Message"
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "MessageFont")
        Case "SmCaption"
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "SmCaptionFont")
        Case "Status"
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "StatusFont")
        Case Else
            ' Return Caption Font
            temp = RegGetString$(HKEY_CURRENT_USER, "Control Panel\desktop\WindowMetrics", "CaptionFont")
    End Select
    GetWindowsSystemFont = temp
    Select Case Parameter
        Case "Name"
            'For Ind = 1 To Len(temp)
            '    Debug.Print Ind, Mid(temp, Ind, 1), Asc(Mid(temp, Ind, 1))
            GetWindowsSystemFont = Mid(temp, 19, InStr(19, temp, Chr(0)) - 19)
        Case "Size"
            GetWindowsSystemFont = Mid(temp, 41, 1) 'InStr(19, temp, Chr(0)) - 19)
        Case "Color"
        Case "Bold"
        Case "Italic"
        Case Else
            GetWindowsSystemFont = Mid(temp, 19, InStr(19, temp, Chr(0)) - 19)
    End Select
End Function

    Function RegGetString$(hInKey As Long, ByVal subkey$, ByVal valname$)

        Dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long
        Dim v$
        Dim R As Long
        RetVal$ = ""
        Const KEY_ALL_ACCESS As Long = &HF0063
        Const ERROR_SUCCESS As Long = 0
        Const REG_SZ As Long = 1
        R = RegOpenKeyEx(hInKey, subkey$, 0, KEY_ALL_ACCESS, hSubKey)
        If R <> ERROR_SUCCESS Then GoTo Quit_Now
        SZ = 256: v$ = String$(SZ, 0)
        R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
        If R = ERROR_SUCCESS And dwType = REG_SZ Then
            RetVal$ = left$(v$, SZ)
            'RetVal$ = "--Not String--" &
            RetVal$ = left$(v$, SZ)
        End If
        If hInKey = 0 Then R = RegCloseKey(hSubKey)
        RegGetString$ = RetVal$
        End Function

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

LVL 12

Accepted Solution

Trygve earned 100 total points
ID: 1971186
Ooops, forgot to mark the question "Answered"

Author Comment

ID: 1971187
Dear Sir,

It works excellent, but looks a bit heavy: "goto" is just a hat-trick. Thank You anyway - it realy is a good job.

Best regards,

LVL 12

Expert Comment

ID: 1971188
You can strip of the parts that you don't need (look in the select case parts of the code. The rest is API stuff. These parts are best programmed by copying from someone elses code :-)

Have a nice day !

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question