Important alert: (current site time 7/16/2013 1:11:09 AM EDT)
 

VB icon

.INI read/write routines

Email
Submitted on: 7/31/1997
By: Newsgroup Posting  
Level: Not Given
User Rating: By 7 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit)
Views: 138964
 
     .INI read/write routines mfncGetFromIni-- Reads from an *.INI file strFileName (full path & file name) mfncWriteIni--Writes to an *.INI file called strFileName (full path & file name) sitush@aol.com
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :.INI read/write routines
'**************************************
Declare Function GetPrivateProfileString Lib "Kernel" (ByVal
lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String,
ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName
As String) As Integer
Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal
lpApplicationName$, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal
lpFileName$)
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: .INI read/write routines
' Description:.INI read/write routines
mfncGetFromIni-- Reads from an *.INI file strFileName (full path & file name)
mfncWriteIni--Writes to an *.INI file called strFileName (full path & file name)
sitush@aol.com
' By: Newsgroup Posting
'
' Returns:mfncGetFromIni--The string stored in [strSectionHeader], line beginning strVariableName
mfncWriteIni--Integer indicating failure (0) or success (other) to write
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=584&lngWId=1'for details.'**************************************

Function mfncGetFromIni (strSectionHeader As String, strVariableName As
String, strFileName As String) As String
'*** DESCRIPTION:Reads from an *.INI file strFileName (full path &
file name)
'*** RETURNS:The string stored in [strSectionHeader], line
beginning strVariableName=
'*** NOTE: Requires declaration of API call
GetPrivateProfileString
'Initialise variable
Dim strReturn As String
'Blank the return string
strReturn = String(255, Chr(0))
'Get requested information, trimming the returned string
mfncGetFromIni = Left$(strReturn,
GetPrivateProfileString(strSectionHeader, ByVal strVariableName, "",
strReturn, Len(strReturn), strFileName))
End Function
Function mfncParseString (strIn As String, intOffset As Integer,
strDelimiter As String) As String
'*** DESCRIPTION:Parses the passed string, returning the value
indicated
'***by the offset specified, eg: the string "Hello,
World",
'***offset 2 = "World".
'*** RETURNS:See description.
'*** NOTE: The offset starts at 1 and the delimiter is the
character
'***which separates the elements of the string.
'Trap any bad calls
If Len(strIn) = 0 Or intOffset = 0 Then
mfncParseString = ""
Exit Function
End If
'Declare local variables
Dim intStartPos As Integer
ReDim intDelimPos(10) As Integer
Dim intStrLen As Integer
Dim intNoOfDelims As Integer
Dim intCount As Integer
Dim strQuotationMarks As String
Dim intInsideQuotationMarks As Integer
strQuotationMarks = Chr(34) & Chr(147) & Chr(148)
intInsideQuotationMarks = False
For intCount = 1 To Len(strIn)
'If character is a double-quote then toggle the In Quotation flag
If InStr(strQuotationMarks, Mid$(strIn, intCount, 1)) <> 0 Then
intInsideQuotationMarks = (Not intInsideQuotationMarks)
End If
If (Not intInsideQuotationMarks) And (Mid$(strIn, intCount, 1) =
strDelimiter) Then
intNoOfDelims = intNoOfDelims + 1
'If array filled then enlarge it, keeping existing contents
If (intNoOfDelims Mod 10) = 0 Then
ReDim Preserve intDelimPos(intNoOfDelims + 10)
End If
intDelimPos(intNoOfDelims) = intCount
End If
Next intCount
'Handle request for value not present (over-run)
If intOffset > (intNoOfDelims + 1) Then
mfncParseString = ""
Exit Function
End If
'Handle boundaries of string
If intOffset = 1 Then
intStartPos = 1
End If
'Requesting last value - handle null
If intOffset = (intNoOfDelims + 1) Then
If Right$(strIn, 1) = strDelimiter Then
intStartPos = -1
intStrLen = -1
mfncParseString = ""
Exit Function
Else
intStrLen = Len(strIn) - intDelimPos(intOffset - 1)
End If
End If
'Set start and length variables if not handled by boundary check above
If intStartPos = 0 Then
intStartPos = intDelimPos(intOffset - 1) + 1
End If
If intStrLen = 0 Then
intStrLen = intDelimPos(intOffset) - intStartPos
End If
'Set the return string
mfncParseString = Mid$(strIn, intStartPos, intStrLen)
End Function
Function mfncWriteIni (strSectionHeader As String, strVariableName As
String, strValue As String, strFileName As String) As Integer
'*** DESCRIPTION:Writes to an *.INI file called strFileName (full
path & file name)
'*** RETURNS:Integer indicating failure (0) or success (other)
to write
'*** NOTE: Requires declaration of API call
WritePrivateProfileString
'Call the API
mfncWriteIni = WritePrivateProfileString(strSectionHeader,
strVariableName, strValue, strFileName)
End Function


Other 153 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/10/1999 6:34:00 AMhyp0r

for some reason its telling me, "File Not Found: Kernel" which is in your global declrations... I tryed kernel32 but it sayed the function wasn't there... if anyone could clue me in it would be helpful :o)

(If this comment was disrespectful, please report it.)

 
6/11/1999 11:06:00 PMLOGIX

Hello, i am trying to write a program , it goes with a ftp server called xitami, i want it to be able to edit ftp user information, this information is in a ini type file, can you please send me a project, and also if you have time, how can i have it list all the different headings of the ini file? since i would not know what they were prior to opening the file.? PLEASE help, thank you
(If this comment was disrespectful, please report it.)

 
6/24/1999 5:30:00 PMdaft_punk

Im having the same problem as hyp0r.
Can someone please tell us what the problem is?
(If this comment was disrespectful, please report it.)

 
6/27/1999 3:20:00 PMn0Pk1LL4

daft_punk and hyp0r:

The declarations for the Kernel32.DLL are the following:

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

(If this comment was disrespectful, please report it.)

 
8/8/1999 4:02:00 PMJason

How do I get the script to read an ini file? I got it to write to the ini file, but I tried reading it into a text box and it didnt work, please email me with coding to help me out. Thank You, Jason
(If this comment was disrespectful, please report it.)

 
1/17/2000 5:05:13 AMTroy

Do a search for Troy Mac on this site and you will find an ini tutorial that may help with some of these questions. Also if you have the API Viewer it is a good tool to use. The API viewer helps you in the fact that you don't need to remember all the syntax for the API's
(If this comment was disrespectful, please report it.)

 
6/9/2000 3:36:07 PMNeoBPI

To long, I could do this in 7 lines of code!
(If this comment was disrespectful, please report it.)

 
7/15/2000 11:18:43 PMplop

i am looking for a way to save and load what is in a textbox, i am new to vb, and if anyone could help me, i woul really apreciate it
(If this comment was disrespectful, please report it.)

 
9/15/2000 12:35:37 AMSkidkid

Plop -
use a rich text box.
i.e
RichTextBox1.Loadfile
(If this comment was disrespectful, please report it.)

 
9/15/2000 12:37:21 AMSKiDKiD

Plop -
RichTextBox1.Loadfile "File"
RichTextBox1.Savefile "File"

(If this comment was disrespectful, please report it.)

 
9/22/2000 4:13:00 PMDave Space

hello im making a program with an options page and i need it to read/write the options from an ini file and make the options take effect once i hit ok/apply how can i do this? my email is morethanska@hotmail.com please reply
(If this comment was disrespectful, please report it.)

 
2/12/2001 1:01:13 AMOtaku Link

um...whenever I type
(If this comment was disrespectful, please report it.)

 
4/21/2001 12:39:42 PMRob Loach

There is a better version of this available on PSC. Search for:

16 and 32 bit functions to create

(If this comment was disrespectful, please report it.)

 
6/10/2001 8:53:36 AMAlex Bunting

why just give us the code, coudl you explain how this works please, im lookign everywhere for info on how to get info and write info to INI files from ext boxes and stuff, and nothing can help, all people write is code that dont help no-one.
(If this comment was disrespectful, please report it.)

 
6/11/2001 5:25:00 AMwo

wo :P
(If this comment was disrespectful, please report it.)

 
7/10/2001 5:05:27 PMSerialZ

yuup :P
(If this comment was disrespectful, please report it.)

 
12/15/2001 4:51:56 PMDiado

There's a function which isn't used (Parse string or something... which can be achieved better using the split function)... but other than that this is 4 lines of code, and works just fine.

Nice 'n' simple.
(If this comment was disrespectful, please report it.)

 
3/5/2002 6:45:21 PM¿?ØØmzØØ¿?

ok check this out i need to know how to get line3 off of my ini and parse to text1.text upon load if any one knows how to do this using filesystemobject please e-mail me.....this is my first comment at psc so pl help
(If this comment was disrespectful, please report it.)

 
7/30/2002 7:53:59 PMzayy

good source
(If this comment was disrespectful, please report it.)

 
12/17/2002 4:23:59 AM

I think it can be done in three lines of code .... and this is too long !
(If this comment was disrespectful, please report it.)

 
11/28/2003 10:55:34 AM


(If this comment was disrespectful, please report it.)

 
1/11/2004 4:39:44 PM

jason: do what n0Pk1LL4 said.
(If this comment was disrespectful, please report it.)

 
4/25/2006 12:35:02 PMAndres

pls can some 1 make an exe off this
(If this comment was disrespectful, please report it.)

 
4/25/2006 12:35:28 PMAndres

pls can some 1 make the source code and upload the file
(If this comment was disrespectful, please report it.)

 
12/1/2007 4:01:52 PMJoel Greene

Put this in your Declarations so your program can work on Win32 and Win16 Systems.

#If Win16 Then
Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal lpApplicationName$, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName$)
#Else
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (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
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
#End If
(If this comment was disrespectful, please report it.)

 
3/28/2009 10:39:31 AMhaxer

good man
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.