Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Code Sample of GetPrivateProfileString in VB5 INI file

Status
Not open for further replies.

Sol9000

Technical User
Aug 12, 2008
2
I lost the harddrive holding all of my VB5 reference sample code and I'm trying to find a good code sample of the GetPrivateProfileString for INI files. I need the whole chunk of code for it, including the defining and assigning of all string values for the GetPrivateProfileString statement.

Example:

Public Function Read_INI()

lpAppName = App.EXEName
lpKeyName = ShellExpression1
lpString = HyperShell1
lpDefault = ""
lpReturnedString = Space(nSize)
nSize = Int(256)
lpFileName = App.Path + "\" + "MultiCast1.INI"

GetPrivateProfileStringA(string lpAppName, string lpKeyName, string lpDefault, lpReturnedString, nSize, string lpFileName);


End Function

I know that this sample of code isn't quite right, but I can't remember how it all went. This is the first programming I've done in a while, but if you could provide an example like the sample I gave above, I would be ecstatic! And remember, this is VB5, not 6.

Thanks!
Sol
 
Here is a copy of the INI class I pieced together in VB6 long ago. Never saw a big need to change it. This should at least get you close to what you need:

Code:
Option Explicit

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 m_sSection As String
Private m_sKey As String
Private m_sININame As String

Public Property Let Section(sSection As String)
     m_sSection = sSection
End Property

Public Property Let Key(sKey As String)
     m_sKey = sKey
End Property

Public Property Let IniFileName(sININame As String)
     m_sININame = sININame
End Property

Public Function ReadINISettings() As String
    Dim sDestination As String * 255
    Dim lReturnVal As Long
    Dim sMsg  As String
    On Error Resume Next
    
    lReturnVal = GetPrivateProfileString(m_sSection, m_sKey, "", sDestination, Len(sDestination), m_sININame)
    'lReturnVal will always equal the length of the returned string not including vbNullChars at the end
    If lReturnVal <> 0 Then
        sDestination = Left(sDestination, InStr(sDestination, Chr(0)) - 1) 'chr(0)=vbNullChar
        ReadINISettings = Trim(sDestination)
    Else
        Err.Raise vbObjectError + 513 + 1001, "CReadINI.ReadINISettings", "Initialization File Error!"
    End If
    If Err.Number <> 0 Then
        sMsg = "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description
        MsgBox sMsg, vbCritical, "Initialization File Error"
    End If
End Function

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
I thank you for your effort, but I need to see how the values are assigned as in the sample I gave. I also need the proper syntax of the GetPrivateProfileString. I wasn't able to translate what you gave into a working solution.

Thank you for trying though. =)

Sol
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top