I am trying to understand a program someone else wrote and don't understand what the following is for

Private Declare Function WritePrivateProfileString Lib _
        "kernel32" Alias "WritePrivateProfileStringA" _
        (ByVal lpApplicationName As String, ByVal _
        lpKeyName As Any, ByVal lpString As Any, ByVal _
        lpFileName As String) As Long
        
Private 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

Private Declare Function WritePrivateProfileSection Lib _
        "kernel32" Alias "WritePrivateProfileSectionA" _
        (ByVal lpAppName As String, ByVal lpString As _
        String, ByVal lpFileName As String) As Long
        
Private Declare Function GetPrivateProfileSection Lib _
        "kernel32" Alias "GetPrivateProfileSectionA" _
        (ByVal lpAppName As String, ByVal lpReturnedString _
        As String, ByVal nSize As Long, ByVal lpFileName _
        As String) As Long
'

Dani AI

Generated

As pointed out, those kernel32 declarations are the classic Win32 helpers for working with INI-format configuration files (the old [Section] / Key=Value text files). They let code read or write single keys or entire sections without manual parsing. These APIs are legacy compatibility functions from 16‑bit Windows, so they still work but are not the recommended config mechanism for new apps.

Behavior notes tied to the four functions:

  • One pair deals with single keys (read/write). The other pair deals with whole sections (read/write) and uses a block of null-separated strings terminated by a double null.
  • The section routines return or expect multi-string buffers (embedded nulls) — you must handle the double-null terminator and check the returned length for truncation.
  • Passing a null/empty pointer for the key or section is how the API deletes keys or sections. Omitting a filename can cause the system to fall back to the system INI (legacy behavior), so always pass an explicit, writable path when you mean a per-app file.

Practical VB tips and pitfalls:

  • The posted declarations alias the ANSI ("A") entry points. On modern Windows prefer the Unicode ("W") entry points or use a managed/config API to avoid conversion issues.
  • Pre-allocate a buffer large enough for GetPrivateProfileString/GetPrivateProfileSection and check the returned length to detect truncation.
  • Writes can be affected by OS caching and file locks; check the return value for success and verify the file path and permissions. For robust, concurrent, or structured settings use the registry, XML/JSON files, or framework config APIs instead of INI functions.

Microsoft reference documentation:
WritePrivateProfileString function (Win32) - Microsoft Docs
GetPrivateProfileString function (Win32) - Microsoft Docs

Hi,

They are All used to read/manipulate "INI" files. See, u can open the textFile and parse, reading character by charecter or Line by line, will take good deal of time.
Instead use these API's, and u can directly manipulate Required Section/String with the new one...

Regards
Veena

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.