调用GetPrivateProfileInt发生异常 “算数运算导致溢出”
zgw 2008-06-30 10:34:47 Public Function ReadIniInt(ByVal Section As String, ByVal key As String) As Integer
这里出问题: ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
代码如下:
Public Class Form1
Const MB_ICONQUESTION As Integer = &H20
Const MB_YESNO As Integer = &H4
Const IDYES As Integer = 6
Const IDNO As Integer = 7
Declare Auto Function MBox Lib "user32.dll" Alias "MessageBox" ( _
ByVal hWnd As Integer, _
ByVal txt As String, _
ByVal caption As String, _
ByVal Typ As Integer) _
As Integer
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 String, _
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 String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Long
Public IniFileName As String
Public ErrorMsg As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Stores the return value.
' Dim RetVal As Integer
' RetVal = MBox(0, "Declare DLL Test", "Windows API MessageBox", _
' MB_ICONQUESTION Or MB_YESNO)
' Check the return value.
' If RetVal = IDYES Then
'MsgBox("You chose Yes")
'Else
'MsgBox("You chose No")
'End If
Dim s As String
s = Application.StartupPath & "\Setup.txt"
MsgBox(s)
SpecifyIni(s)
Dim i As Integer
Try
i = ReadIniInt("GETLOG", "GETLOG_WAITMAXSEC")
MsgBox(i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub SpecifyIni(ByVal FilePathName)
IniFileName = Trim(FilePathName)
End Sub
Public Function NoIniFile() As Boolean
NoIniFile = True
If IniFileName = vbNullString Then
ErrorMsg = ".INI File not Found !"
Exit Function
End If
ErrorMsg = vbNullString
NoIniFile = False
End Function
Public Function WriteIniString(ByVal Section As String, ByVal key As String, ByVal Value As String) As Boolean
WriteIniString = False
If NoIniFile() Then
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then
ErrorMsg = "Fail writting to the .ini file !"
Exit Function
End If
WriteIniString = True
End Function
Public Function ReadIniString(ByVal Section As String, ByVal key As String, ByVal Size As Long) As String
Dim returnstr As String
Dim ReturnLng As Long
ReadIniString = vbNullString
If NoIniFile() Then
Exit Function
End If
returnstr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, returnstr, Size, IniFileName)
' ReadIniString = Trim$(Left(returnstr, ReturnLng))
ReadIniString = Replace(ReadIniString, Chr(0), "")
End Function
Public Function ReadIniInt(ByVal Section As String, ByVal key As String) As Integer
Dim ReturnLng As Integer
ReadIniInt = 0
ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)
If ReturnLng = 1 Then
ErrorMsg = "Fail reading the .ini file !"
Exit Function
End If
End If
ReadIniInt = ReturnLng
End Function
End Class