?
Solved

Ini Read and Ini Write

Posted on 2000-03-20
8
Medium Priority
?
600 Views
Last Modified: 2012-06-27
I am trying to get the following two functions to work within VB V5.0.
   A) GetPrivateProfileString
   B) WritePrivateProfileString

Right now I am working with the "GetPrivateProfileString", and have no success at it.  Yes, I have searched EE under the text 'GetPrivateProfileString' and picked off the second best VB example, and tried to implement that idea into my VB program.  And still no success.  Currently I am running under Win95-Vers B.  I am not into the Registry bit, and that is why I using the INI file.  I believe, before I upgraded to Win95-Version B, I was running under Win95-Version A and I was using these two function within a VC++ DLL file that I put together - and it is still working well. I haven't rebuilt the DLL File since my system upgrade.  Am I going to run into some trouble with my VC++ DLL functions with Version B?

But any rate, can get a copy of  examples of these functions to work.   I'll enclose my function code below...

Function GetSysIni(ByVal section As String, ByVal skey As String) As String
    Dim retVal As String
    Dim defstrg As String
    Dim AppName As String
    Dim KeyName As String
    Dim worked As Long
    Dim lnsize As Long
    Dim filedir As String
    Dim WinPath As String
   
   
'' 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
       
    AppName = section
    KeyName = skey
    WinPath = WindowsDirectory()
    filedir = UCase(WinPath & "\" & "System.ini")
    retVal = ""
    lnsize = 255
    defstrg = Chr(0)
'   lpApplicationName, lpKeyName, lpDefault, lpRetunedString, nSize, lpFileName)
    worked = GetPrivateProfileString(AppName, KeyName, defstrg, retVal, lnsize, filedir)
    If worked = 0 Then
        GetSysIni = "unknown"
    Else
        GetSysIni = Left(retVal, InStr(retVal, Chr(0)) - 1)
    End If
End Function

Are they any drivers that I need to install to make it work??

See what you can do...

 Thanks in Advance...

 Midnightexpress
0
Comment
Question by:midnightexpress
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 14

Expert Comment

by:wsh2
ID: 2638819
You will find a very good writeup here.. along with Source code that you can use.

http://www.thescarms.com/vbasic/registry.htm

Be sure to save the web page.. as the documentation for the module is on it.
0
 

Expert Comment

by:raquel061097
ID: 2638831
Here's a bit of code you can use:

WriteProfile is straight forward. GetProfile is a bit more coded. Hope this helps.

-start-
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
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 Const ENUM_BUF_SIZE As Integer = 80

Private Function GetProfile(sSection As String, sKey As String, sIniPath) As String
    Dim retLen As Long
    Dim buf As String * ENUM_BUF_SIZE
   
    If sKey <> "" Then
        retLen = GetPrivateProfileString(sSection, sKey, "", buf, ENUM_BUF_SIZE, sIniPath)
    Else
        retLen = GetPrivateProfileString(sSection, 0&, "", buf, ENUM_BUF_SIZE, sIniPath)
    End If
    If retLen = 0 Then
        GetProfile = ""
    Else
        GetProfile = Left$(buf, retLen)
    End If
End Function


Sub SomeSub()
    Dim nResult As Integer
    nResult = WritePrivateProfileString(sSection, sEntry, sValue, sIniPath)
    If nResult = 0 Then
        'Some error - Use GetLastError
    End If
End Sub
0
 

Expert Comment

by:ozcustom
ID: 2638888
The answer to your problem is to NOT write calls to the functions all the time - rather cxreate a wrapper so you can make a call to something that you know works; all the time..

Here it is - these two functions,
ReadIni and WriteIni are wrappers that I have used for the last four years - upgrading as I went - they work...

Copy all this code to a code module, and then just make calls to the wrappers...


Public 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 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
Public 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


Public Function ReadIni(ByVal fileName As String, ByVal Section As String, ByVal KeyName As String, Optional bInteger As Variant, Optional vDefault As Variant) As Variant
'=================================================
'    Copyright © 1998 Australian Custom Software Pty Ltd
'-------------------------------------------------
'Author     : Tony Harris

'Created    :
'Precis     : Wrapper around the ini file API to read
'             in from a a private ini file.
'           Boolean declares whether it is an Integer or not
'           vDefault as the default value
'-------------------------------------------------

'Modified   :
'=================================================
    Dim bValid As Boolean
    Dim bFileFound As Boolean
    Dim BString As Variant
    Dim sReturn As String
    Dim sDefault As String
    Dim nReturn As Long
    Dim nDefault As Long
    Dim nSize As Long
    On Error GoTo ReadErr
    bValid = True
    bFileFound = True
    BString = True
    If Len(Trim(fileName)) = 0 Then bValid = False
    If Len(Dir(fileName)) = 0 Then
        bValid = False
        bFileFound = False
    End If
    If Len(Trim(Section)) = 0 Then bValid = False
    If Len(Trim(KeyName)) = 0 Then bValid = False
    If IsMissing(bInteger) Then
        BString = True
    Else
        BString = Not (CBool(bInteger))
    End If

StartRead:
    If bValid Then
        If Not BString Then
            If IsMissing(vDefault) Then
                nDefault = 0
            Else
                nDefault = CInt(vDefault)
            End If
            nReturn = GetPrivateProfileInt(Section, KeyName, nDefault, fileName)
            ReadIni = nReturn
        Else
            If IsMissing(vDefault) Then
                sDefault = ""
            Else
                sDefault = CStr(vDefault)
            End If
            sReturn = Space(255)
            nSize = Len(sReturn)
            nReturn = GetPrivateProfileString(Section, KeyName, sDefault, sReturn, nSize, fileName)
            If nReturn > 0 Then
                sReturn = Left(sReturn, nReturn)
            Else
                sReturn = sDefault
            End If
            ReadIni = sReturn
        End If
    Else
        If bFileFound Then
            MsgBox " some appropriate message"
            ReadIni = "Error"
        Else
            MsgBox "some other appropriate message"
            ReadIni = "Error"
        End If
    End If
    On Error GoTo 0
    Exit Function

ReadErr:
    Select Case Err.Number
    Case 71 'Disk Not ready
        bFileFound = False
        bValid = False
        Resume StartRead
    Case Else
        MsgBox Err, vbOKOnly, "Error in Reading INI File"
    End Select
End Function


Public Sub WriteIni(ByVal fileName As String, ByVal Section As String, ByVal KeyName As String, ByVal sEntry As String)
'=================================================
'    Copyright © 1998 Australian Custom Software Pty Ltd
'-------------------------------------------------
'Author     : Tony Harris

'Created    :
'Precis     : Wrapper around the ini file API to write
'             out to a private ini file.
'-------------------------------------------------

'Modified   :
'=================================================
    Dim bValid As Boolean
    Dim nReturnLen As Long
    bValid = True
    If Len(Trim(fileName)) = 0 Then bValid = False
    If Len(Trim(Section)) = 0 Then bValid = False
    If Len(Trim(KeyName)) = 0 Then bValid = False
    If Len(Trim(sEntry)) = 0 Then sEntry = ""
    If bValid Then
        nReturnLen = WritePrivateProfileString(Section, KeyName, sEntry, fileName)
    Else
        MsgBox "some appropriate error message"
    End If
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:midnightexpress
ID: 2642818
One more question regarding your code implementation.  I copied over your examples, and ran the 'getprivateprofilestring'.  And each time I altered the section and key names at different runs. All of my runs, the return value is allways zero(0).  The only thing I altered was to make 'sinipath' to say:  sinipath as string.  

 What haven't I set right within my VB V5.0 environment? Within VC++, have used this function and it has worked.  

 Can you shed any light on my problem(s)??

  Thanks,

 Midnightexpress
0
 

Expert Comment

by:ozcustom
ID: 2651340
make sure you have "option explicit" at the beginning of the module...

also make the call using the defined structure - don't change anything...

0
 

Accepted Solution

by:
raquel061097 earned 200 total points
ID: 2651718
raquel changed the proposed answer to a comment
0
 

Author Comment

by:midnightexpress
ID: 2651897
Thanks for your expertise.
0
 

Expert Comment

by:BobJacobson
ID: 7652253
Does anyone know how to read more that 255 characters of an entry in an INI file?
 
          Bob
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

801 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