Solved

INI Files - Storing & Retrieving Info

Posted on 1998-07-29
2
267 Views
Last Modified: 2012-06-27
I want to store the path of a database in the INI file of a program.
What is the best way to Create an entry in an INI file to store the database path. What is the best way to then retrieve this from the INI file.
0
Comment
Question by:trevorm
2 Comments
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1467071
Here is the way on how to handle INI File

gsTemp = GetPrivateProfileString("Directories", "Temp", "c:\temp", gsIniFile)


Option Explicit

'---------------------------------------------------------------
'-INI API Declarations...
'---------------------------------------------------------------
Declare Function OSGetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare Function OSGetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function OSGetPrivateProfileString 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 OSWritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function OSWritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Declare Function OSGetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long
Declare Function OSGetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Declare Function OSGetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Declare Function OSWriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
Declare Function OSWriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long

Private Const nBUFSIZEINI = 1024
Private Const nBUFSIZEINIALL = 4096

Public Function GetPrivateProfileString(ByVal szSection As String, ByVal szEntry As Variant, ByVal szDefault As String, ByVal szFileName As String) As String
   ' *** Get an entry in the inifile ***
   
   Dim szTmp                     As String
   Dim nRet                      As Long
   
   If (IsNull(szEntry)) Then
       ' *** Get names of all entries in the named Section ***
      szTmp = String$(nBUFSIZEINIALL, 0)
      nRet = OSGetPrivateProfileString(szSection, 0&, szDefault, szTmp, nBUFSIZEINIALL, szFileName)
   Else
       ' *** Get the value of the named Entry ***
      szTmp = String$(nBUFSIZEINI, 0)
      nRet = OSGetPrivateProfileString(szSection, CStr(szEntry), szDefault, szTmp, nBUFSIZEINI, szFileName)
   End If
   GetPrivateProfileString = Left$(szTmp, nRet)
   
End Function

Public Sub WritePrivateProfile(ByVal szSection As String, ByVal szEntry As Variant, ByVal vValue As Variant, ByVal szFileName As String)
   ' *** Write an szEntry in the inifile ***
   
   Dim nRet                      As Long

   If (IsNull(szEntry)) Then
      ' *** Delete the section ***
      nRet = OSWritePrivateProfileString(szSection, 0&, 0&, szFileName)
   ElseIf (IsNull(vValue)) Then
      ' *** Delete the entry in the section ***
      nRet = OSWritePrivateProfileString(szSection, CStr(szEntry), 0&, szFileName)
   Else
      ' *** Insert or replace the Entry ***
      nRet = OSWritePrivateProfileString(szSection, CStr(szEntry), CStr(vValue), szFileName)
   End If
   
End Sub

Public Function GetProfileString(ByVal szSection As String, ByVal szEntry As Variant, ByVal szDefault As String) As String
   ' *** Get an entry in the WIN inifile ***
   
   Dim szTmp                    As String
   Dim nRet                     As Long

   If (IsNull(szEntry)) Then
       ' *** Get names of all entries in the named Section ***
       szTmp = String$(nBUFSIZEINIALL, 0)
       nRet = OSGetProfileString(szSection, 0&, szDefault, szTmp, nBUFSIZEINIALL)
   Else
       ' *** Get the value of the named Entry ***
       szTmp = String$(nBUFSIZEINI, 0)
       nRet = OSGetProfileString(szSection, CStr(szEntry), szDefault, szTmp, nBUFSIZEINI)
   End If
   GetProfileString = Left$(szTmp, nRet)
   
End Function


Sub WriteProfile(ByVal szSection As String, ByVal szEntry As Variant, ByVal vValue As Variant)
   ' *** Write an szEntry in the WIN inifile ***
   
   Dim nRet                      As Long
   
   If (IsNull(szEntry)) Then
      ' *** Delete the section ***
       nRet = OSWriteProfileString(szSection, 0&, 0&)
   ElseIf (IsNull(vValue)) Then
      ' *** Delete the entry in the section ***
       nRet = OSWriteProfileString(szSection, CStr(szEntry), 0&)
   Else
      ' *** Insert or replace the Entry ***
       nRet = OSWriteProfileString(szSection, CStr(szEntry), CStr(vValue))
   End If
   
End Sub



0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1467072
The best way (only way in vb5) is to use GetPrivateProfileString and WritePrivateProfileString API.  Here are functions that use them, and make it a little easier:

Option Explicit

#If Win16 Then
    Private Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Integer
#Else
    Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, 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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
#End If

Public Function ReadINI_Integer(szAppName As String, szKeyname As String, szININame As String, Optional varDefault) As Integer
    Dim iResult As Integer
    iResult = GetPrivateProfileInt(szAppName, szKeyname, IIf(IsMissing(varDefault), 0, CInt(varDefault)), szININame)
   
    ReadINI_Integer = iResult
End Function

Public Function ReadINI_String(szAppName As String, szKeyname As String, szININame As String, Optional varDefault) As String
    Const c_iLargest As Integer = 255
   
    Dim szReturn      As String
    Dim iSizeReturn   As Integer
    Dim szDefault     As String
   
    If IsMissing(varDefault) Then
        szDefault = ""
    Else
        szDefault = CStr(varDefault)
    End If
   
    szReturn = Space$(c_iLargest)
       
    'Read from the INI File
    iSizeReturn = GetPrivateProfileString(szAppName, szKeyname, szDefault, szReturn, c_iLargest, szININame)
   
    'trim Read value and removed NULL
    szReturn = Trim$(Left$(szReturn, iSizeReturn))
   
    ReadINI_String = szReturn
End Function

Public Function WriteINI(szAppName As String, szKeyname As String, szValue As String, szININame As String)
    WriteINI = WritePrivateProfileString(szAppName, szKeyname, szValue, szININame)
End Function


Here are example functions on how to use them.  These functions read/write the window coordinates:

Public Sub GetWindowPosition(frmCur As Form, szINIFile As String)
    Dim iLeft   As Integer
    Dim iTop    As Integer
    Dim iHeight As Integer
    Dim iWidth  As Integer
   
    iLeft = ReadINI_Integer(frmCur.Name, "Left", szINIFile)
    If iLeft > 0 Then
        frmCur.Left = iLeft
    End If
   
    iTop = ReadINI_Integer(frmCur.Name, "Top", szINIFile)
    If iTop > 0 Then
        frmCur.Top = iTop
    End If
   
    iHeight = ReadINI_Integer(frmCur.Name, "Height", szINIFile)
    If iHeight > 0 Then
        frmCur.Height = iHeight
    End If
   
    iWidth = ReadINI_Integer(frmCur.Name, "Width", szINIFile)
    If iWidth > 0 Then
        frmCur.Width = iWidth
    End If
End Sub




Public Sub SaveWindowPosition(frmCur As Form, szINIFile As String)
    WriteINI frmCur.Name, "Left", frmCur.Left, szINIFile
    WriteINI frmCur.Name, "Top", frmCur.Top, szINIFile
    WriteINI frmCur.Name, "Width", frmCur.Width, szINIFile
    WriteINI frmCur.Name, "Height", frmCur.Height, szINIFile
End Sub


0

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

Title # Comments Views Activity
Input past end of file vbs script 9 82
using Access 8 59
VB6 - Compare and highlight cell not the same 3 44
Child Form in front 4 39
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

895 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

15 Experts available now in Live!

Get 1:1 Help Now