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
Public 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
'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'WriteValue:值
Public Function WriteIni(FileName As String, _
PathName As String, _
KeyName As String, _
WriteValue As String) As Long
Dim Rc As Long
Rc = WritePrivateProfileString(PathName, KeyName, WriteValue, FileName)
WriteIni = Rc
End Function'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'Default:默认字符
Public Function ReadIni(FileName As String, _
PathName As String, _
KeyName As String, _
Optional Default As String = vbNullString, _
Optional DataLen As Long = &H100) As String
Dim Rc As Long
Dim TempStr As String
If DataLen <= 0 Then DataLen = &H100
TempStr = String$(DataLen, Chr$(0))
Rc = GetPrivateProfileString(PathName, KeyName, Default, TempStr, DataLen, FileName)
If Rc > 0 Then
ReadIni = StrConv(LeftB(StrConv(TempStr, vbFromUnicode), Rc), vbUnicode)
Else
ReadIni = Default
End If
End Function
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Public 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
'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'WriteValue:值
Public Function WriteIni(FileName As String, _
PathName As String, _
KeyName As String, _
WriteValue As String) As Long
Dim Rc As Long
Rc = WritePrivateProfileString(PathName, KeyName, WriteValue, FileName)
WriteIni = Rc
End Function'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'Default:默认字符
Public Function ReadIni(FileName As String, _
PathName As String, _
KeyName As String, _
Optional Default As String = vbNullString, _
Optional DataLen As Long = &H100) As String
Dim Rc As Long
Dim TempStr As String
If DataLen <= 0 Then DataLen = &H100
TempStr = String$(DataLen, Chr$(0))
Rc = GetPrivateProfileString(PathName, KeyName, Default, TempStr, DataLen, FileName)
If Rc > 0 Then
ReadIni = StrConv(LeftB(StrConv(TempStr, vbFromUnicode), Rc), vbUnicode)
Else
ReadIni = Default
End If
End Function
Dim tmpString As String
On Error GoTo ReadWriteINIError
ReadWriteINI = "OK"
If IsNull(Mode) Or Len(Mode) = 0 Then
ReadWriteINI = "ERROR MODE" ' Set the return value
Exit Function
End If
If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
ReadWriteINI = "ERROR Secname" ' Set the return value
Exit Function
End If
If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
ReadWriteINI = "ERROR Keyname" ' Set the return value
Exit Function
End If
filename = "C:\Vbasic\Test\WinPlace.ini" ' <<<<< put your file name here
If UCase(Mode) = "WRITE" Then
If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = 0 Then
ReadWriteINI = "ERROR KeyValue"
Exit Function
Else
secname = tmpSecname
keyname = tmpKeyname
keyvalue = tmpKeyValue
anInt = WritePrivateProfileString(secname, keyname, keyvalue,filename)
End If
End If
If UCase(Mode) = "GET" Then
secname = tmpSecname
keyname = tmpKeyname
defaultkey = "Failed"
keyvalue = String$(50, 32)
anInt = GetPrivateProfileString(secname, keyname, defaultkey, keyvalue, Len(keyvalue), filename)
If Left(keyvalue, 6) <> "Failed" Then
' *** got it
tmpString = keyvalue
tmpString = RTrim(tmpString)
tmpString = Left(tmpString, Len(tmpString) - 1)
End If
ReadWriteINI = tmpString
End If
Exit Function
ReadWriteINIError:
MsgBox Error Stop
End Function
Option Explicit
'写INI文件的API函数,分别是读和写
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 AppName As String, ByVal KeyName As String, ByVal keydefault As String, ByVal filename As String) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
'read from the ini file
Public Function myReadINI(ByVal inifile As String, ByVal inisection As String, ByVal inikey As String, ByVal iniDefault As String) As String'Fail fracefully if no file / wrong file is specified.
'If no section (appname), default is first appname
'if no key, default is first key
Dim lpApplicationName As String
Dim lpKeyName As String
Dim lpDefault As String
Dim lpReturnedString As String
Dim nSize As Long
Dim lpFileName As String
Dim retval As Long
Dim filename As String
Dim strtemp As String
lpDefault = Space$(254)
lpDefault = iniDefault lpReturnedString = Space$(254) nSize = 254
lpFileName = inifile
lpApplicationName = inisection
lpKeyName = inikey
filename = lpFileName
retval = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)
strtemp = Trim(lpReturnedString)
myReadINI = Left(strtemp, Len(strtemp) - 1)
End Function
'write to ini file
Public Function myWriteINI(ByVal inifile As String, ByVal inisection As String, ByVal inikey As String, ByVal Info As String) As String
Dim retval As Long
retval = WritePrivateProfileString(inisection, inikey, Info, inifile)
myWriteINI = LTrim$(Str$(retval))
End Function