Adam Caudill

Security Leader, Researcher, Developer, Writer, & Photographer

Simple INI API

This post was imported from an old blog archive, and predates the creation of AdamCaudill.com.

While storing settings in a INI file is no longer in vogue, so developers (including myself) still find value in this old method of save a program’s settings. The advantages of using INI files instead of the system registry may be numerous, one of the main reasons I use it is portability, by saving a single file I can move the entire settings for my program to any other computer.

My games also use INI files instead of using the registry, this allows them to perform their own user management instead of allow Windows to define how to handle it.

Option Explicit

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, _
    ByVal lpFileName As String _
    ) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
    ByVal AppName As String, _
    ByVal KeyName As String, _
    ByVal keydefault As String, _
    ByVal FileName As String _
    ) As Long

Function INIGetKeyVal(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String) As String
Dim strRetData As String
Dim lngRetVal As Long

    If Dir$(strFileName) <> "" Then
        strRetData = String$(255, 0)
        lngRetVal = GetPrivateProfileString(strSection, strKey, "", strRetData, Len(RetVal), strFileName)
        If lngRetVal > 0 Then
            INIGetKeyVal = Left$(strRetData, InStr(strRetData, Chr$(0)) - 1)
        End If
    End If
End Function

Function INISetKeyVal(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String, ByVal strValue As String) As Boolean
Dim lngRetVal As Long

    lngRetVal = WritePrivateProfileString(strSection, strKey, strValue, strFileName)
    INISetKeyVal = (lngRetVal <> 0)
End Function

Adam Caudill


Related Posts

  • CloseApp

    This post was imported from an old blog archive, and predates the creation of AdamCaudill.com. This is a useful function to close a program based on a windows caption, this should work for any top-level window. Paste all this into a standard module, save it. Then call CloseApp("Notepad") or whatever the name of the window is, it’s nice & simple and should close the program instantly. This requires Windows 2000 plus, for older versions of Windows a different method is required, that isn’t covered here, seeing as Windows 2000 and better require special privileges to forcefully close a program.

  • UPEK Windows Password Decryption

    On August 28th ElcomSoft announced that they had determined a method to extract Windows passwords from the registry for users of UPEK’s fingerprint readers and Protector Suite software (UPEK is now owned by AuthenTec, which is now owned by Apple). What they didn’t announce was the technical details of how they did it. Myself and Brandon Wilson have been working to recreate their research – and we have. We have not been in contact with ElcomSoft, so this is an independent re-discovery of this vulnerability.

  • HP Folio 13

    When Intel and various industry partners started talking about “ultra-books” as competition against Apple and tablets, I was more than a little skeptical. Ultra-books are small and light weight – but not cheap (average price being around $1,000) and rather underpowered compared to what you can get for the same money with a more traditional laptop (they are basically MacBook Air knock-offs). I had written them off almost as soon as they were announced.

  • Running RegEdit as SYSTEM

    While facing an interesting research challenge, digging into the inner working of Windows, I realized that I needed to change a registry value. That’s simple enough, I fire up RegEdit, make the change, then politely as RegEdit knows how, it told me that I couldn’t change the value! Being one that hates when my computer tells me I can’t do something, I had to find another option. So after some research, I found my answer: sc.