VB读取ini文件的函数

Option Explicit

Private Declare Function GetPrivateProfileInt Lib "kernel32" _
Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) As Long

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

Public ErrorMsg As String

Private Sub Class_Initialize()
ErrorMsg = vbNullString
End Sub

'写入----------------------------------
Public Function WriteString(iniFileName As String, Section As String, key As String, Value As String) As Boolean
WriteString = False
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, iniFileName) = 0 Then
ErrorMsg = "Failed to write to the ini file!"
Exit Function
End If
WriteString = True
End Function

'读出字符串----------------------------
Public Function ReadString(iniFileName As String, Section As String, key As String, Size As Long) As String
Dim ReturnStr As String
Dim ReturnLng As Long
ErrorMsg = vbNullString
ReadString = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnStr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, iniFileName)
ReadString = Left(ReturnStr, ReturnLng)
End Function

'读出数值-----------------------------
Public Function ReadInt(iniFileName As String, Section As String, key As String) As Long
Dim ReturnLng As Long
ReadInt = 0
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnLng = GetPrivateProfileInt(Section, key, 0, iniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, iniFileName)
If ReturnLng = 1 Then
ErrorMsg = "Can not read the ini file!"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值