• Welcome to Valhalla Legends Archive.
 

Need help using INI files

Started by Sorc_Polgara, June 23, 2004, 08:03 PM

Previous topic - Next topic

Sorc_Polgara

I already know how to use the WritePrivateProfileString and GetPrivateProfileString API stuff... and that isn't my problem.

I need to know if there is a way to delete a [section] from an INI file  or a way to overwrite/replace a [section] from an INI file in VB6?

If not, is there perhaps another way I could go about doing so...

I've tried creating FileSystemObject TextStream object thingy, you know, the VBScript way of Reading, Writing, and Appending a file.  However I can't seem overwrite or delete the line with the [section] without deleting the whole text/INI file.  I can't use the SkipLine method either...

Is there a way of using the FileSystemObject TextStream object thingy and I am just doing something wrong?  Is there some other way of doing File I/O which would work?

I'm open to any way you think I might be able to accomplish this.  Thanks


I'm kind of limited on what different APIs and Object usages out there... btw, I only learned about the GetPrivateProfileString thingy because it was a module that someone made -_-...  and I'm a noob to this forum :\

dxoigmn

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that.  Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.

Sorc_Polgara

Quote from: dxoigmn on June 23, 2004, 08:22 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that.  Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

Quote
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.

Stealth

If worse comes to worst, read the whole file in, erase the parts you don't want, and spit it back out.
- Stealth
Author of StealthBot

dxoigmn

Quote from: Sorc_Polgara on June 23, 2004, 08:40 PM
Quote from: dxoigmn on June 23, 2004, 08:22 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that.  Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

Quote
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.

Delete the whole section including keys?  Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".

Newby

QuoteNote  This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

Sorc_Polgara

Quote from: dxoigmn on June 23, 2004, 09:41 PM
Quote from: Sorc_Polgara on June 23, 2004, 08:40 PM
Quote from: dxoigmn on June 23, 2004, 08:22 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that.  Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

Quote
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.

Delete the whole section including keys?  Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".

Ok, I'll try using the WritePrivateProfileSection method.  I'm not quite sure on how exactly I would implement the WritePrivateProfileSection method in VB though.

I'm guessing that I would do the same thing as using the WritePrivateProfileString method which I'm using from a module...


Private Declare Function WriteIniInfo Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long


I'd do basically the same thing as this?


Private Declare Function WriteIniSection Lib "kernal32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long


Would this be correct?

dxoigmn

Quote from: Sorc_Polgara on June 24, 2004, 07:16 AM
Quote from: dxoigmn on June 23, 2004, 09:41 PM
Quote from: Sorc_Polgara on June 23, 2004, 08:40 PM
Quote from: dxoigmn on June 23, 2004, 08:22 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that.  Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

Quote
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.

Delete the whole section including keys?  Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".

Ok, I'll try using the WritePrivateProfileSection method.  I'm not quite sure on how exactly I would implement the WritePrivateProfileSection method in VB though.

I'm guessing that I would do the same thing as using the WritePrivateProfileString method which I'm using from a module...


Private Declare Function WriteIniInfo Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long


I'd do basically the same thing as this?


Private Declare Function WriteIniSection Lib "kernal32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long


Would this be correct?


That looks correct.  Additionally, you could use to API Text Viewer (located in the tools directory) to get the Function declaration of WritePrivateProfileSection; there are however off-by-1 bugs in some of the Type declarations it generates.

Sorc_Polgara

This is a totally different question but still concerns INI handling in VB6

I searched the MSDN Library and found a function called GetPrivateProfileSectionNames that I want to use.  However, it is not available for use in the API Text Viewer like the rest of the Profile functions.  Can it still be used/declared?

I mean, using the other working INI functions like GetPrivateProfileSection that I declared and wrote Public functions for as a template, I went and tried to declare it myself.  However it doesn't seem to work...

Here is my declaration and function which I put in my module.  It is almost identical to GetPrivateProfileSection and GetPrivateProfileString which I have put in my module and both work.


Private Declare Function GetIniSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnedBuffer As String, ByVal nSize As Long, lpFileName As String) As Long

Public Function GetSectionNames(FileName$)
   Dim INIBuffer As String
   Dim INIBufferSize As Long
   Dim BytesReturned As Integer
   
   INIBuffer = Space$(1024)
   INIBufferSize = Len(INIBuffer)
   BytesReturned = GetIniSectionNames(INIBuffer, INIBufferSize, FileName$)
   If BytesReturned = 0 Then
       GetSectionNames = ""
   Else
       GetSectionNames = Mid$(INIBuffer, 1, BytesReturned)
   End If
End Function


This is how I try and use it and again, is almost identical to the other two.


ReturnedString = GetSectionNames(iniFileName)


The string "iniFileName" is a working directory which I also use with the GetPrivateProfileString and GetPrivateProfileSection functions successfully.

Is it some flaw in my code, or is it what I am thinking it is... that it cannot be declared in VB6...

Eli_1

lpFilename needs to be passed 'ByVal'.

Sorc_Polgara

Quote from: dxoigmn on June 23, 2004, 09:41 PM
Quote from: Sorc_Polgara on June 23, 2004, 08:40 PM
Quote from: dxoigmn on June 23, 2004, 08:22 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that.  Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

Quote
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.

Delete the whole section including keys?  Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".

Yes it works!  Thanks

Quote
Posted by: Eli_1  Posted on: June 29, 2004, 05:08 pm  
lpFilename needs to be passed 'ByVal'.

Yes, fixes it too!  Thanks