Solved

Ini Read and Ini Write

Posted on 2000-03-20
8
589 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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 50 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

808 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