调用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
...全文
226 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanlongwuhui 2008-06-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zdbb 的回复:]
.Net中:
Long型要定义为Integer
[/Quote]
API函数声明的时候类型需要修改下
只在此山中 2008-06-30
  • 打赏
  • 举报
回复
Private Declare Function GetPrivateProfileInt Lib "kernel32" _
Alias "GetPrivateProfileIntA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Integer, _
ByVal lpFileName As String) As Integer
只在此山中 2008-06-30
  • 打赏
  • 举报
回复
.Net中:
Long型要定义为Integer
神之泪花 2008-06-30
  • 打赏
  • 举报
回复
路过,关注中....

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧