VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Easy and Fast Way to Read Value from ini file using API Calls

by Raghuraja. C (21 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 6th January 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Easy and Fast Way to Read Value from ini file using API Calls

API Declarations


API Calls Used:

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

Public DATABASE AS String
Public USERNAME AS String
Public PASSWORD AS String


Rate Easy and Fast Way to Read Value from ini file using API Calls




File Name:  DatabaseInfo.ini

[DataBaseUsed]

'Database Base Name
Database = "DBTOUSE"

'Valid User Name to Access
Username = "UserName01"

'Valid Password to access
Password = "Password01"

How to use:

fnGetValueFromIniFile "DataBaseUsed", "Database", DATABASE, DatabaseInfo.ini
fnGetValueFromIniFile "DataBaseUsed", "UserName", USERNAME, DatabaseInfo.ini
fnGetValueFromIniFile "DataBaseUsed", "Password", PASSWORD, DatabaseInfo.ini

'==============================================================================
Public Function fnGetValueFromIniFile (ByVal strApplicationName As String, ByVal strKeyName As String, ByRef strReturnedString As String, ByVal strFileName As String) As Boolean
    On Error GoTo LOCALERRORHANDLER
    Dim lngRc1           As Long                      'To store return value
    Dim lngRc2           As Long                      'To store return value
    Dim strCheckBuff     As String                    'To store and Check buffer
    Dim strDefault       As String                    'Default value
    '******************************************************
    fnGetIniString = False
    strReturnedString = String(261, 0)
    strDefault = vbNullString

    strFileName = App.Path + "\" + strFileName
    lngRc1 = GetPrivateProfileString(strApplicationName, strKeyName, strDefault, strReturnedString, 261, strFileName)

    If lngRc1 < 1 Then Exit Function

    strReturnedString = Left(strReturnedString, lngRc1)

    lngRc2 = lngRc1
    strCheckBuff = Chr(0)
    lngRc1 = InStr(1, strReturnedString, strCheckBuff, vbTextCompare)

    If lngRc1 > 0 Then
        lngRc2 = lngRc1 - 1
        strReturnedString = Left(strReturnedString, lngRc2)
    End If

    fnGetValueFromIniFile = True

    Exit Function
LOCALERRORHANDLER:
End Function
'==============================================================================


Download this snippet    Add to My Saved Code

Easy and Fast Way to Read Value from ini file using API Calls Comments

No comments have been posted about Easy and Fast Way to Read Value from ini file using API Calls. Why not be the first to post a comment about Easy and Fast Way to Read Value from ini file using API Calls.

Post your comment

Subject:
Message:
0/1000 characters