A very useful module for reading and writing INI files in VB6

Source: Internet
Author: User

A very useful module for reading and writing INI files in VB6
New module (registry not recommended) named rwini
The INI file may encounter an error when there is a carriage return or line break. After testing, the number of Chinese characters must be smaller than 86 bytes, and the value of English letters must be smaller than 143 bytes to return the list box. (This is my previous code, which records the content of the list box)
Option explicit
Public inifilename as string
Public declare function getprivateprofileint lib "Kernel32" alias "getprivateprofileinta" (byval lpapplicationname as string, byval lpkeyname as string, byval ndefault as long, byval lpfilename as string)
Long
Public declare function getprivateprofilestring lib "Kernel32" alias "getprivateprofilestringa" (byval lpapplicationname as string, byval lpkeyname as any, byval lpdefault as string, byval lpreturnedstring
String, byval nsize as long, byval lpfilename as string) as long

Public 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

'*************************************** * Obtain the INI string value (function) **************************************** **

Function getinis (byval sectionname as string, byval keyword as string, byval defstring as string) as string

Dim resultstring as string * 144, temp as integer
Dim s as string, I as integer
Temp % = getprivateprofilestring (sectionname, keyword, "", resultstring, 144, appprofilename (inifilename ))

'Retrieve the keyword Value
If TEMP %> 0 then' keyword value is not empty
S = ""
For 1 to 144
If ASC (mid $ (resultstring, I, 1) = 0 then
Exit
Else
S = S & Mid $ (resultstring, I, 1)
End if
Next
Else
Temp % = writeprivateprofilestring (sectionname, keyword, defstring, appprofilename (inifilename ))

'Write the default value to the INI file.
S = defstring
End if
Getinis = s
End Function

* INI value (function) **************************************** ***********

Function getinin (byval sectionname as string, byval keyword as string, byval defvalue as long) as integer

Dim D as long, s as string
D = defvalue
Getinin = getprivateprofileint (sectionname, keyword, defvalue, appprofilename (inifilename ))

If D <> defvalue then
S = "" & D
D = writeprivateprofilestring (sectionname, keyword, S, appprofilename (inifilename ))

End if
End Function

'*************************************** Write string value (sub) **************************************** **********

Sub setinis (byval sectionname as string, byval keyword as string, byval valstr as string)

Dim res %
Res % = writeprivateprofilestring (sectionname, keyword, valstr, appprofilename (inifilename ))

End sub
'*************************************** * Write value (sub) **************************************** **************

Sub setinin (byval sectionname as string, byval keyword as string, byval valint as long)

Dim res %, S $
S $ = STR $ (valint)
Res % = writeprivateprofilestring (sectionname, keyword, S $, appprofilename (inifilename ))

End sub

''This is when I don't know how to clear a key (keyword)
The write process for clearing string values is implemented by using the write function to write an empty value. 'sub delinis (byval sectionname as string, byval keyword as string)

'Dim retval as integer
'Retval = writeprivateprofilestring (sectionname, keyword, "", appprofilename (inifilename ))

'End sub
'Actually, 0' indicates that the previous one is cleared. I write another "". If it is clearing a section, it will write less than one key and more than one "".

'*************************************** Clear keyword "key" (sub) **************************************** *********

Sub delinikey (byval sectionname as string, byval keyword as string)

Dim retval as integer
Retval = writeprivateprofilestring (sectionname, keyword, 0 &, appprofilename (inifilename ))

End sub

'If you want to clear the section, write less one key and more than one '.
* Section "section" (sub) **************************************** *******

Sub delinisec (byval sectionname as string) 'clear Section
Dim retval as integer
Retval = writeprivateprofilestring (sectionname, 0 &, "", appprofilename (inifilename ))

End sub

************************************* Define the ini file Name (function) **************************************** ***********

'Define INI File Name
Function appprofilename (inifilename)
Appprofilename = app. Path & "\" & inifilename & ". ini"
End Function

######################################## ###############################

'Usage: first define inifilename = "file name" without the INI suffix
'That is to say, you can assign a value to inifilename to write records, and you can write different INI files at any time (regardless of whether the file already exists) by modifying this public variable.

'Then the delinikey (byval sectionname as string, byval keyword as string) is cleared.

'Delinisec (byval sectionname as string) purge
'Setinin (byval sectionname as string, byval keyword as string, byval valint as long) Write count

'Getinin (byval sectionname as string, byval keyword as string, byval defvalue as long) read count

'Setinis (byval sectionname as string, byval keyword as string, byval valstr as string) write characters

'Getinis (byval sectionname as string, byval keyword as string, byval valstr as string) read characters

The call example is as follows:

Sub rinin ()
Dim initemp as string
Initemp = inifilename 'Save the original INI File Name
Inifilename = app. exename 'is written to another INI file. App. exename is the name of your program.
If getinin ("lstbackup", "backupnumber", 0) <lstbackup. listcount then

'The third parameter "0" indicates that the default value returned when the specified key value is not found is "0"
Setinin "lstbackup", "backupnumber", lstbackup. listcount
'......
End if
Inifilename = initemp 'continue to use the original INI File
End sub

Create and read a. ini file

Author: CWW

After entering Win95, The INI file is generally replaced by the read/write registry, but we can still
The traditional method of reading and writing INI files in win31 stores the current settings of the program.
Read back. Currently, we recommend that you use the getsetting savesetting method to store data in the registry.
. Storage program settings
'Put 3 textbox and a commandbox in form.
Private 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
Private 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

Private sub commandementclick ()
Dim success as long
Success = writeprivateprofilestring ("MyApp", "text1", text1.text, "C: \ AA. ini ")

'Parameter 1 section name
'Parameter 2: Project in. ini
'Parameter THREE PROJECT CONTENT
'Parameter number 4. ini file name
Success = writeprivateprofilestring ("MyApp", "text2", text2.text, "C: \ AA. ini ")

Success = writeprivateprofilestring ("myapp2", "text3", text3.text, "C: \ AA. ini ")

End sub

Private sub form_load ()
Dim RET as long
Dim buff as string
Buff = string (255, 0)
Ret = getprivateprofilestring ("MyApp", "text1", "text1", buff, 256, "C: \ AA. ini ")

'If. ini MyApp does not contain text1, the parameter value is three.
Text1.text = buff
Buff = string (255, 0)
Ret = getprivateprofilestring ("MyApp", "text2", "text2", buff, 256, "C: \ AA. ini ")

Text2.text = buff
Buff = string (255, 0)
Ret = getprivateprofilestring ("myapp2", "text3", "text3", buff, 256, "C: \ AA. ini ")

Text3.text = buff
End sub

I used to learn how to create a role. I have been using it all the time.
Method: First, write in the module:
1. 'create the AAA. ini file in the D. keystore root project.

'Module Worker Program
'Module in progress
Option explicit

'-------------------------
'Xiaoming
Declare function writeprivateprofilestring lib "Kernel32" alias "writeprivateprofilestringa" (byval lpapplicationname as string, byval lpkeyname as any, byval lpstring as any, byval lpfilename as string)
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

'Additional settings
Public Function mygetsetting (section as string, keyname as string, defaultvalue as string) as string

Dim X as long
Dim holder as string * 255

X = getprivateprofilestring (section, keyname, defaultvalue, holder, 254, "d: \ AAA. ini ")

Mygetsetting = left $ (holder, instr (holder, CHR $ (0)-1)

End Function

'Save settings

Public sub mysetsetting (section as string, keyname as string, keyValue as string)

Dim X as long

X = writeprivateprofilestring (section, keyname, keyValue, "d: \ AAA. ini ")

End sub

2. Form:

Private sub form_load ()

Dim strserver as string
Dim struid as string
Dim strpwd as string
Dim strdbname as string
Dim strconstring as string

Strserver = mygetsetting ("server name", "server name ","")
Struid = mygetsetting ("use pipeline name", "use pipeline name ","")
Strpwd = mygetsetting ("password", "password ","")
Strdbname = mygetsetting ("data register name", "data register name ","")


Strconstring = "driver = {SQL Server}; network library = TCP/IP sockets; server =" & strserver & "; uid =" & struid &"; pwd = "& strpwd &"; database = "& strdbname &""


Conn. myconn. commandtimeout = 30
Conn. myconn. Open strconstring


End sub

Call the mygetsetting function, read the information from the INI file in from_load, and then concatenate the server name, user name, password, and database name into the string, and then open

3. Execute the SQL statement:
Private sub commandementclick ()
Dim RS as ADODB. recordset
Dim SQL as string
SQL = "select * from student"
Set rs = conn. myconn. Execute (SQL)
If Rs. recordcount> 0 then
Set tdbgrid2.datasource = rs
End if
End sub

4. Verification Code:
Write INI files
Private sub command3_click ()
'Save changes
Mysetsetting "server name", "server name", "mis25"
Mysetsetting "use region name", "Use region name ",""
Mysetsetting "ting"," ",""
Mysetsetting "data register name", "data register name", "hh"
Msgbox "saved successfully"

End sub
5. Read the content in the INI file.
Private sub command4_click ()
'Retrieve the variation
Msgbox mygetsetting ("server name", "server name ","")
Msgbox mygetsetting ("using the producer name", "using the producer name ","")
Msgbox mygetsetting ("", "","")
Msgbox mygetsetting ("data register name", "data register name ","")

End sub

6. If you want to save it in the current path: change the code above:
'Additional settings
Public Function mygetsetting (section as string, keyname as string, defaultvalue as string) as string

Dim X as long
Dim holder as string * 255

'X = getprivateprofilestring (section, keyname, defaultvalue, holder, 254, "d: \ AAA. ini ")

X = getprivateprofilestring (section, keyname, defaultvalue, holder, 254, app. Path & "\ AAA. ini ")


Mygetsetting = left $ (holder, instr (holder, CHR $ (0)-1)

When the end function reads the content in the current path, it also changes the above Code

'Save settings

Public sub mysetsetting (section as string, keyname as string, keyValue as string)

Dim X as long

'X = writeprivateprofilestring (section, keyname, keyValue, "d: \ AAA. ini ")

X = writeprivateprofilestring (section, keyname, keyValue, app. Path & "\ AAA. ini ")

End sub

1. Set the username and password for accessing the database in the sqlserver database:
Open sqlserver ------ Select User under database ---- right-click ---- new database user --- New
Enter the user name and password (twice)
2. Write the INI file and record the username and password in the INI file:
3. You only need to copy and INI files when using the tool, because the program sets to read the INI files in the current path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.