Solved

Ini Read and Ini Write

Posted on 2000-03-20
8
578 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now