Tutorial :Is there a way to read an .ini file into memory and then read particular values from it?


I've got a situation where an ini file is in memory (in a string variable) and I'd like to read values out of it without writing the ini file data to disk.

UPDATE:This is data that I do not want to write to the HD. I'm downloading it from a web server into memory and then getting some data.

Is there any way to do that in VB6? Maybe with a Win API call?



Check out this article at DevX.com

Read/Write INI without using API

This should get you pointed in a good direction. The modules are a bit dirty and do, at this point, require a path for the INI to be stored. Instead simply modify the modules to use your string directly and you should receive the desired result. Let me know how this works out for you.


The ini file is to be stored on hard disk. If you want to save in registry instead (this should be added to the registry file too), you can use these functions:

SaveSetting  GetSetting  


If you don't want to save it on disk I think it might be difficult to use any specific API calls. But if it's an ini file it should be in a structured format, why not just loop through it until the section you want and then read the values you want from it.

It's been a long time but it should be something like this I think:

Just Split it on newlines to get an array of lines, then each section should start in a certain format and for each line in there you check if it contains an = I think, and if so everything to the left of the first = is the name of the value and everything to the right of it is the value.


Declare the two Windows API function imports

Public Declare Function GetPrivateProfileStringA Lib "kernel32.dll" (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 WritePrivateProfileStringA Lib "kernel32.dll" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long   

For reading

sReturn = Space(255)  sReturn = Left(sReturn, GetPrivateProfileStringA(header, key, defaultReturn, sReturn, 255, filePath))  

For writting

WritePrivateProfileStringA header, key, Datum, filePath  

Remember the structure of an INI file is

[header]  key=data   


Yes, almost anything is possible in VB6. But I don't think there is any windows API to do it.

If you can't find a library to do it, you will have to code it your self. If you have to code it your self, see if you can find some existing code that works with ini files so you know how to work with ini files.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »