Writing INI section failing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Honek

    Writing INI section failing

    Can anyone spot why the below function is failing?

    It returns true but when looking inside the INI file there's no new added
    section.

    ****CODE****

    Public Function WriteINIFileSec tion(ByVal sEntryType As String, ByVal
    sKeyName As String, ByVal sFilePath As String) As Boolean

    '************** *************** *************** *************** *

    ' +++ Purpose

    '************** *************** *************** *************** *

    ' Writes a section to an INI file

    '************** *************** *************** *************** *

    'Declare variables

    Dim Response As Integer

    Try

    'Attempt to write the data to the INI file

    Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,
    sFilePath)

    'The API call failed

    If Response = 0 Then

    'Return indicating function has failed

    WriteINIFileSec tion = False

    Else

    'Return indicating function has successfully completed

    WriteINIFileSec tion = True

    End If

    'If there are any errors catch them

    Catch e As Exception

    MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Cri tical,
    "Writing account settings section")

    'Return indicating function has failed

    WriteINIFileSec tion = False

    Exit Function

    End Try

    End Function


  • Tom Shelton

    #2
    Re: Writing INI section failing


    Adam Honek wrote:[color=blue]
    > Can anyone spot why the below function is failing?
    >
    > It returns true but when looking inside the INI file there's no new added
    > section.
    >
    > ****CODE****
    >
    > Public Function WriteINIFileSec tion(ByVal sEntryType As String, ByVal
    > sKeyName As String, ByVal sFilePath As String) As Boolean
    >
    > '************** *************** *************** *************** *
    >
    > ' +++ Purpose
    >
    > '************** *************** *************** *************** *
    >
    > ' Writes a section to an INI file
    >
    > '************** *************** *************** *************** *
    >
    > 'Declare variables
    >
    > Dim Response As Integer
    >
    > Try
    >
    > 'Attempt to write the data to the INI file
    >
    > Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,
    > sFilePath)
    >
    > 'The API call failed
    >
    > If Response = 0 Then
    >
    > 'Return indicating function has failed
    >
    > WriteINIFileSec tion = False
    >
    > Else
    >
    > 'Return indicating function has successfully completed
    >
    > WriteINIFileSec tion = True
    >
    > End If
    >
    > 'If there are any errors catch them
    >
    > Catch e As Exception
    >
    > MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Cri tical,
    > "Writing account settings section")
    >
    > 'Return indicating function has failed
    >
    > WriteINIFileSec tion = False
    >
    > Exit Function
    >
    > End Try
    >
    > End Function[/color]

    Well the problem is your call to WritePrivatePro fileString. You are
    calling it with lpKeyName set to nothing. That's equivalent to a null
    pointer and if you read the doc's on WritePrivatePro fileString - if you
    pass a null in this parameter your telling the function to delete the
    section.

    You probably want to call it with a dummy name and value, and then
    delete the value also with WritePrivatePro fileString, just pass the
    section and key name and then pass vbNullString in for the value
    parameter, that will delete the key, but leave the new section.

    --
    Tom Shelton [MVP]

    Comment

    • tommaso.gastaldi@uniroma1.it

      #3
      Re: Writing INI section failing


      you have:
      Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,

      sFilePath)

      with Nothing and vbNullString as entries. Perhaps you wanted to pass
      some arguments.

      see here:


      -tom

      Adam Honek ha scritto:
      [color=blue]
      > Can anyone spot why the below function is failing?
      >
      > It returns true but when looking inside the INI file there's no new added
      > section.
      >
      > ****CODE****
      >
      > Public Function WriteINIFileSec tion(ByVal sEntryType As String, ByVal
      > sKeyName As String, ByVal sFilePath As String) As Boolean
      >
      > '************** *************** *************** *************** *
      >
      > ' +++ Purpose
      >
      > '************** *************** *************** *************** *
      >
      > ' Writes a section to an INI file
      >
      > '************** *************** *************** *************** *
      >
      > 'Declare variables
      >
      > Dim Response As Integer
      >
      > Try
      >
      > 'Attempt to write the data to the INI file
      >
      > Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,
      > sFilePath)
      >
      > 'The API call failed
      >
      > If Response = 0 Then
      >
      > 'Return indicating function has failed
      >
      > WriteINIFileSec tion = False
      >
      > Else
      >
      > 'Return indicating function has successfully completed
      >
      > WriteINIFileSec tion = True
      >
      > End If
      >
      > 'If there are any errors catch them
      >
      > Catch e As Exception
      >
      > MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Cri tical,
      > "Writing account settings section")
      >
      > 'Return indicating function has failed
      >
      > WriteINIFileSec tion = False
      >
      > Exit Function
      >
      > End Try
      >
      > End Function[/color]

      Comment

      • Adam Honek

        #4
        Re: Writing INI section failing

        Hmmm according to what you're saying (if I understand you
        correctly) you're saying to do it in two API calls?

        I based the code on someone else's that lists this for writing
        a section without keys or values (just the section name in square brackets)

        Even if I do it as the code below, it just doesn't write to the file but
        returns true.
        Public Function AddINISection(B yVal sSection As String, ByVal sFilename As
        String, Optional ByVal sVariableName As String = Nothing) As Boolean

        Try

        WritePrivatePro fileString(sSec tion, sVariableName, vbNullString, sFilename)

        Return True

        Catch ex As Exception

        Return False

        End Try

        End Function

        Adam

        "Tom Shelton" <tom@mtogden.co m> wrote in message
        news:1145401298 .905667.247090@ g10g2000cwb.goo glegroups.com.. .[color=blue]
        >
        > Adam Honek wrote:[color=green]
        >> Can anyone spot why the below function is failing?
        >>
        >> It returns true but when looking inside the INI file there's no new added
        >> section.
        >>
        >> ****CODE****
        >>
        >> Public Function WriteINIFileSec tion(ByVal sEntryType As String, ByVal
        >> sKeyName As String, ByVal sFilePath As String) As Boolean
        >>
        >> '************** *************** *************** *************** *
        >>
        >> ' +++ Purpose
        >>
        >> '************** *************** *************** *************** *
        >>
        >> ' Writes a section to an INI file
        >>
        >> '************** *************** *************** *************** *
        >>
        >> 'Declare variables
        >>
        >> Dim Response As Integer
        >>
        >> Try
        >>
        >> 'Attempt to write the data to the INI file
        >>
        >> Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,
        >> sFilePath)
        >>
        >> 'The API call failed
        >>
        >> If Response = 0 Then
        >>
        >> 'Return indicating function has failed
        >>
        >> WriteINIFileSec tion = False
        >>
        >> Else
        >>
        >> 'Return indicating function has successfully completed
        >>
        >> WriteINIFileSec tion = True
        >>
        >> End If
        >>
        >> 'If there are any errors catch them
        >>
        >> Catch e As Exception
        >>
        >> MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Cri tical,
        >> "Writing account settings section")
        >>
        >> 'Return indicating function has failed
        >>
        >> WriteINIFileSec tion = False
        >>
        >> Exit Function
        >>
        >> End Try
        >>
        >> End Function[/color]
        >
        > Well the problem is your call to WritePrivatePro fileString. You are
        > calling it with lpKeyName set to nothing. That's equivalent to a null
        > pointer and if you read the doc's on WritePrivatePro fileString - if you
        > pass a null in this parameter your telling the function to delete the
        > section.
        >
        > You probably want to call it with a dummy name and value, and then
        > delete the value also with WritePrivatePro fileString, just pass the
        > section and key name and then pass vbNullString in for the value
        > parameter, that will delete the key, but leave the new section.
        >
        > --
        > Tom Shelton [MVP]
        >[/color]


        Comment

        • Adam Honek

          #5
          Re: Writing INI section failing

          OK thanks I fixed it using a two step approach using dummy keyname and value
          then deleting the keyname and value thus leaving only the section name.

          Adam

          <tommaso.gastal di@uniroma1.it> wrote in message
          news:1145402396 .157064.25590@i 40g2000cwc.goog legroups.com...[color=blue]
          >
          > you have:
          > Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,
          >
          > sFilePath)
          >
          > with Nothing and vbNullString as entries. Perhaps you wanted to pass
          > some arguments.
          >
          > see here:
          > http://www.vbexplorer.com/VBExplorer...tutorial_2.asp
          >
          > -tom
          >
          > Adam Honek ha scritto:
          >[color=green]
          >> Can anyone spot why the below function is failing?
          >>
          >> It returns true but when looking inside the INI file there's no new added
          >> section.
          >>
          >> ****CODE****
          >>
          >> Public Function WriteINIFileSec tion(ByVal sEntryType As String, ByVal
          >> sKeyName As String, ByVal sFilePath As String) As Boolean
          >>
          >> '************** *************** *************** *************** *
          >>
          >> ' +++ Purpose
          >>
          >> '************** *************** *************** *************** *
          >>
          >> ' Writes a section to an INI file
          >>
          >> '************** *************** *************** *************** *
          >>
          >> 'Declare variables
          >>
          >> Dim Response As Integer
          >>
          >> Try
          >>
          >> 'Attempt to write the data to the INI file
          >>
          >> Response = WritePrivatePro fileString(sEnt ryType, Nothing, vbNullString,
          >> sFilePath)
          >>
          >> 'The API call failed
          >>
          >> If Response = 0 Then
          >>
          >> 'Return indicating function has failed
          >>
          >> WriteINIFileSec tion = False
          >>
          >> Else
          >>
          >> 'Return indicating function has successfully completed
          >>
          >> WriteINIFileSec tion = True
          >>
          >> End If
          >>
          >> 'If there are any errors catch them
          >>
          >> Catch e As Exception
          >>
          >> MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Cri tical,
          >> "Writing account settings section")
          >>
          >> 'Return indicating function has failed
          >>
          >> WriteINIFileSec tion = False
          >>
          >> Exit Function
          >>
          >> End Try
          >>
          >> End Function[/color]
          >[/color]


          Comment

          • Tom Shelton

            #6
            Re: Writing INI section failing


            Adam Honek wrote:[color=blue]
            > Hmmm according to what you're saying (if I understand you
            > correctly) you're saying to do it in two API calls?
            >
            > I based the code on someone else's that lists this for writing
            > a section without keys or values (just the section name in square brackets)
            >
            > Even if I do it as the code below, it just doesn't write to the file but
            > returns true.[/color]

            When you pass a key name and a vbNullString for the value, your telling
            the API call to delete the key.

            Yes you have to do it in a two step aproach

            --
            Tom Shelton [MVP]

            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Writing INI section failing

              "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:[color=blue]
              > Can anyone spot why the below function is failing?
              >
              > It returns true but when looking inside the INI file there's no new added
              > section.[/color]

              A working INI file reading/writing wrapper can be found here:

              <URL:http://www.mentalis.or g/soft/class.qpx?id=6>

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://classicvb.org/petition/>

              Comment

              Working...