如何:调用采用无符号类型的 Windows 函数 (Visual Basic)

如果您使用类,模块或具有无符号整数的成员的结构类型,则可以访问具有 Visual Basic的这些成员。

调用采用无符号类型的 windows 函数

  1. 使用来 Declare 语句 调用 Visual Basic 哪库包含该函数,确定其名称在库中,确定其调用序列以及将字符串,调用时。

  2. 在 Declare 语句,请使用 UInteger、 ULong、 UShort或 Byte 以适合使用无符号类型的每个参数。

  3. 参考要调用的常数的名称和值它使用的 windows 函数的文档。 其中的许多在 WinUser.h 文件中定义的。

  4. 在代码中声明必要的常数。 许多 windows 常数是 32 位无符号值,因此,您应声明这些 AsUInteger。

  5. 以常规方法调用函数。 下面的示例调用 windows 函数 MessageBox,采用无符号整数参数。

    Public Class windowsMessage
        Private Declare Auto Function mb Lib "user32.dll" Alias "MessageBox" (
            ByVal hWnd As Integer, 
            ByVal lpText As String, 
            ByVal lpCaption As String, 
            ByVal uType As UInteger) As Integer
        Private Const MB_OK As UInteger = 0
        Private Const MB_ICONEXCLAMATION As UInteger = &H30
        Private Const IDOK As UInteger = 1
        Private Const IDCLOSE As UInteger = 8
        Private Const c As UInteger = MB_OK Or MB_ICONEXCLAMATION
        Public Function messageThroughWindows() As String
            Dim r As Integer = mb(0, "Click OK if you see this!", 
                "Windows API call", c)
            Dim s As String = "Windows API MessageBox returned " &
                 CStr(r)& vbCrLf & "(IDOK = " & CStr(IDOK) &
                 ", IDCLOSE = " & CStr(IDCLOSE) & ")"
            Return s
        End Function
    End Class
    

    您可以使用以下代码测试函数 messageThroughWindows 。

    Public Sub consumeWindowsMessage()
        Dim w As New windowsMessage
        w.messageThroughWindows()
    End Sub
    

    警告

    UInteger、 ULong、 UShort和 SByte 数据类型不是 (cls) 的 公共语言规范 一部分,因此,符合 CLS 的代码不能使用使用其组件。

    安全说明安全说明

    调用非托管代码,例如 windows 应用程序编程接口 (API) (api),您的代码会有潜在的安全风险。

    安全说明安全说明

    调用 windows API 需要非托管代码权限,这可能会影响其在部分信任的情况下执行。有关更多信息,请参见 SecurityPermission代码访问权限

请参见

任务

演练:调用 Windows API (Visual Basic)

参考

数据类型摘要 (Visual Basic)

Integer 数据类型 (Visual Basic)

UInteger 数据类型

Declare 语句