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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

948 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now