16 and 32 bit functions to create

By | 2002-06-01

16 AND 32 bit functions to read/write ini files–very useful!

Original Author: VB Qaid

API Declarations

‘****************************************************
‘* INI_sm.BAS *
‘****************************************************
Option Explicit
#If Win16 Then
Declare Function WritePrivateProfileString Lib “Kernel” (ByVal AppName As String, ByVal KeyName As String, ByVal NewString As String, ByVal filename As String) As Integer
Declare Function GetPrivateProfileString Lib “Kernel” Alias “GetPrivateProfilestring” (ByVal AppName As String, ByVal KeyName As Any, ByVal default As String, ByVal ReturnedString As String, ByVal MAXSIZE As Integer, ByVal filename As String) As Integer
#Else
‘ NOTE: The lpKeyName argument for GetProfileString, WriteProfileString,
‘ GetPrivateProfileString, and WritePrivateProfileString can be either
‘ a string or NULL. This is why the argument is defined as “As Any”.
‘ For example, to pass a string specify ByVal “wallpaper”
‘ To pass NULL specify ByVal 0&
‘ You can also pass NULL for the lpString argument for WriteProfileString
‘ and WritePrivateProfileString
‘ Below it has been changed to a string due to the ability to use vbNullString
Declare Function GetPrivateProfileString Lib “kernel32” Alias “GetPrivateProfileStringA” (ByVal lpApplicationName As String, ByVal lpKeyName As String, 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 lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
#End If

Code

Create a new module called: INI_SM.BAS
Add an attribute:
Attribute VB_Name = "ini_sm"
Add this code:
'*******************************************************
'* Procedure Name: sReadINI              *
'*=====================================================*
'*Returns a string from an INI file. To use, call the *
'*functions and pass it the Section, KeyName and INI  *
'*File Name, [sRet=sReadINI(Section,Key1,INIFile)].  *
'*val command.                     *
'*******************************************************
Function ReadINI(Section, KeyName, filename As String) As String
    Dim sRet As String
    sRet = String(255, Chr(0))
    ReadINI = Left(sRet, GetPrivateProfileString(Section, ByVal KeyName, "", sRet, Len(sRet), filename))
End Function
'*******************************************************
'* Procedure Name: WriteINI              *
'*=====================================================*
'*Writes a string to an INI file. To use, call the   *
'*function and pass it the sSection, sKeyName, the New *
'*String and the INI File Name,            *
'*[Ret=WriteINI(Section,Key,String,INIFile)].     *
'*Returns a 1 if there were no errors and       *
'*a 0 if there were errors.              *
'*******************************************************
Function writeini(sSection As String, sKeyName As String, sNewString As String, sFileName) As Integer
    Dim r
    r = WritePrivateProfileString(sSection, sKeyName, sNewString, sFileName)
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.