Writing INI section failing

A

Adam Honek

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
 
T

Tom Shelton

Adam said:
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.
 
A

Adam Honek

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
 
A

Adam Honek

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
 
T

Tom Shelton

Adam said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top