赛迪网 > IT技术 编程语言 > 文章
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

直接从系统得到错误描述

发布时间:2006.08.16 03:04     来源:plwww    作者:

'作者: Thierry Waty
'作者主页: http://www.geocities.com/ResearchTriangle/6311/
'这是一个根据错误代码直接从系统中得到错误描述的程序,你可以不要用硬编码了

'使用举例:

'     Call APIError

       ' *** Or
 '      Debug.Print ReturnAPIError(53)
      ' *** Return : 网络适配器硬件出错。


' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site     : www.geocities.com/ResearchTriangle/6311/
' * E-Mail       : waty.thierry@usa.net
' * Date         : 12/10/1998
' * Time         : 20:20
' * Module Name  : APIError_Module
' * Module Filename  : APIError.bas
' **********************************************************************
' * Comments     :
' * 这是一个根据错误代码直接从系统中得到错误描述的程序,你可以不要用硬编码
' *
' *
' **********************************************************************

Option Explicit

Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200

Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
    (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
    ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, _
    Arguments As Long) As Long

' *** Status Codes
Private Const INVALID_HANDLE_VALUE = -1&
Private Const ERROR_SUCCESS = 0&

Public Function ReturnAPIError(ErrorCode As Long) As String
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site     : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail       : waty.thierry@usa.net
   ' * Date         : 12/10/1998
   ' * Time         : 20:21
   ' * Module Name      : APIError_Module
   ' * Module Filename  : APIError.bas
   ' * Procedure Name   : ReturnAPIError
   ' * Parameters       :
   ' *            ErrorCode As Long
   ' **********************************************************************
   ' * Comments     :
   ' * Takes an API error number, and returns
   ' * a descriptive text string of the error
   ' *
   ' **********************************************************************

   Dim sBuffer    As String

   ' *** Allocate the string, then get the system to
   ' *** tell us the error message associated with
   ' *** this error number
  
   sBuffer = String(256, 0)
   FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0&, ErrorCode, 0&, sBuffer, Len(sBuffer), 0&

   ' *** Strip the last null, then the last CrLf pair if it exists
  
   sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
   If Right$(sBuffer, 2) = Chr$(13) & Chr$(10) Then
      sBuffer = Mid$(sBuffer, 1, Len(sBuffer) - 2)
   End If

   ReturnAPIError = sBuffer

End Function

Public Sub ApiError()
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site     : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail       : waty.thierry@usa.net
   ' * Date         : 12/10/1998
   ' * Time         : 20:35
   ' * Module Name      : APIError_Module
   ' * Module Filename  : APIError.bas
   ' * Procedure Name   : APIError
   ' * Parameters       :
   ' **********************************************************************
   ' * Comments     :
   ' * Takes an API error number, and returns
   ' * a descriptive text string of the error
   ' *
   ' **********************************************************************

   Dim sError   As String
  
   On Error GoTo ERROR_APIError
  
   sError = InputBox("Enter the error number", "Returns API error")
  
   If IsNumeric(sError) = False Then Exit Sub
  
   MsgBox ReturnAPIError(CLng(sError)), vbInformation + vbOKOnly, "Error n " & sError
  
   Exit Sub
  
ERROR_APIError:
   MsgBox "Error n " & sError & vbCrLf & " Invalid error number" & vbCrLf & "You have to give another one", vbCritical + vbOKOnly, "Error n " & sError
  
End Sub



 


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· 2、全局变量和枚举常量 (07-01) · 7、支持函数 (01-08)
· 6、ExportToGIF函数 (11-14) · 5、AddDataSeries函数 (07-26)
· 3、基本函数和事件 (09-26) · 1、组件结构概览 (07-06)
· 怎么让窗体总在前 (01-13) · VB环境下压缩数据流播放技术(1) (03-25)
· 在VB中使用文件对象 (08-27) · VB中获取逻辑磁盘的信息 (11-06)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
[政务][电信][金融][农业][制造业][中小企业]
[CIO][ERP][协同][IT管理][中间件][电子商务]
[政策][地方][专家][评估][辞典][博客][社区]
· 专题:一路畅通构想曲——让出行不再遭遇堵车
· CIO工作亲历:企业ERP选型不能忽视"选人关"
· 综述:信息化建设给中国监狱带来的各种变化
· 金融业风险管理和法规遵从有五点需考虑的因素
· 保险业CIO关注:该如何建立统一高效的CRM体系
· 调查显示:多数CIO对IT规划仍存在困惑和误解
  博客·论坛 ·曾剑秋·项立刚·Java学习·网管