|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Writing INI section failingIt returns true but when looking inside the INI file there's no new added section. ****CODE**** Public Function WriteINIFileSection(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 = WritePrivateProfileString(sEntryType, Nothing, vbNullString, sFilePath) 'The API call failed If Response = 0 Then 'Return indicating function has failed WriteINIFileSection = False Else 'Return indicating function has successfully completed WriteINIFileSection = True End If 'If there are any errors catch them Catch e As Exception MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Critical, "Writing account settings section") 'Return indicating function has failed WriteINIFileSection = False Exit Function End Try End Function Adam Honek wrote:
Show quoteHide quote > Can anyone spot why the below function is failing? Well the problem is your call to WritePrivateProfileString. You are> > It returns true but when looking inside the INI file there's no new added > section. > > ****CODE**** > > Public Function WriteINIFileSection(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 = WritePrivateProfileString(sEntryType, Nothing, vbNullString, > sFilePath) > > 'The API call failed > > If Response = 0 Then > > 'Return indicating function has failed > > WriteINIFileSection = False > > Else > > 'Return indicating function has successfully completed > > WriteINIFileSection = True > > End If > > 'If there are any errors catch them > > Catch e As Exception > > MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Critical, > "Writing account settings section") > > 'Return indicating function has failed > > WriteINIFileSection = False > > Exit Function > > End Try > > End Function calling it with lpKeyName set to nothing. That's equivalent to a null pointer and if you read the doc's on WritePrivateProfileString - 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 WritePrivateProfileString, 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] 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(ByVal sSection As String, ByVal sFilename As String, Optional ByVal sVariableName As String = Nothing) As Boolean Try WritePrivateProfileString(sSection, sVariableName, vbNullString, sFilename) Return True Catch ex As Exception Return False End Try End Function Adam Show quoteHide quote "Tom Shelton" <t**@mtogden.com> wrote in message news:1145401298.905667.247090@g10g2000cwb.googlegroups.com... > > Adam Honek wrote: >> 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 WriteINIFileSection(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 = WritePrivateProfileString(sEntryType, Nothing, vbNullString, >> sFilePath) >> >> 'The API call failed >> >> If Response = 0 Then >> >> 'Return indicating function has failed >> >> WriteINIFileSection = False >> >> Else >> >> 'Return indicating function has successfully completed >> >> WriteINIFileSection = True >> >> End If >> >> 'If there are any errors catch them >> >> Catch e As Exception >> >> MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Critical, >> "Writing account settings section") >> >> 'Return indicating function has failed >> >> WriteINIFileSection = False >> >> Exit Function >> >> End Try >> >> End Function > > Well the problem is your call to WritePrivateProfileString. You are > calling it with lpKeyName set to nothing. That's equivalent to a null > pointer and if you read the doc's on WritePrivateProfileString - 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 WritePrivateProfileString, 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] > Adam Honek wrote:
> Hmmm according to what you're saying (if I understand you When you pass a key name and a vbNullString for the value, your telling> 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. the API call to delete the key. Yes you have to do it in a two step aproach -- Tom Shelton [MVP] you have:
Response = WritePrivateProfileString(sEntryType, Nothing, vbNullString, sFilePath) with Nothing and vbNullString as entries. Perhaps you wanted to pass some arguments. see here: http://www.vbexplorer.com/VBExplorer/focus/ini_tutorial_2.asp -tom Adam Honek ha scritto: Show quoteHide quote > 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 WriteINIFileSection(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 = WritePrivateProfileString(sEntryType, Nothing, vbNullString, > sFilePath) > > 'The API call failed > > If Response = 0 Then > > 'Return indicating function has failed > > WriteINIFileSection = False > > Else > > 'Return indicating function has successfully completed > > WriteINIFileSection = True > > End If > > 'If there are any errors catch them > > Catch e As Exception > > MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Critical, > "Writing account settings section") > > 'Return indicating function has failed > > WriteINIFileSection = False > > Exit Function > > End Try > > End Function 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.gasta***@uniroma1.it> wrote in message Show quoteHide quote news:1145402396.157064.25590@i40g2000cwc.googlegroups.com... > > you have: > Response = WritePrivateProfileString(sEntryType, Nothing, vbNullString, > > sFilePath) > > with Nothing and vbNullString as entries. Perhaps you wanted to pass > some arguments. > > see here: > http://www.vbexplorer.com/VBExplorer/focus/ini_tutorial_2.asp > > -tom > > Adam Honek ha scritto: > >> 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 WriteINIFileSection(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 = WritePrivateProfileString(sEntryType, Nothing, vbNullString, >> sFilePath) >> >> 'The API call failed >> >> If Response = 0 Then >> >> 'Return indicating function has failed >> >> WriteINIFileSection = False >> >> Else >> >> 'Return indicating function has successfully completed >> >> WriteINIFileSection = True >> >> End If >> >> 'If there are any errors catch them >> >> Catch e As Exception >> >> MsgBox("The following error occured: " + e.Message, MsgBoxStyle.Critical, >> "Writing account settings section") >> >> 'Return indicating function has failed >> >> WriteINIFileSection = False >> >> Exit Function >> >> End Try >> >> End Function > "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb: A working INI file reading/writing wrapper can be found here:> 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. <URL:http://www.mentalis.org/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/>
Emailing with default client
Regular Expression Help...Line Break CreateObject internet access Reference different versions of an ActiveX-dll checking for element in xml document Is it possible to compile an image file into a DLL or EXE file? 1.1 and 2.0 .Net framework in same application? Dynamically reading structure fields Two way collection Free unzip plug-in for VB.NET? |
|||||||||||||||||||||||