Udostępnij za pośrednictwem


How to: Call a Windows Function that Takes Unsigned Types (Visual Basic)

If you are consuming a class, module, or structure that has members of unsigned integer types, you can access these members with Visual Basic.

To call a Windows function that takes an unsigned type

  1. Use a Declare Statement to tell Visual Basic which library holds the function, what its name is in that library, what its calling sequence is, and how to convert strings when calling it.

  2. In the Declare statement, use UInteger, ULong, UShort, or Byte as appropriate for each parameter with an unsigned type.

  3. Consult the documentation for the Windows function you are calling to find the names and values of the constants it uses. Many of these are defined in the WinUser.h file.

  4. Declare the necessary constants in your code. Many Windows constants are 32-bit unsigned values, and you should declare these As UInteger.

  5. Call the function in the normal way. The following example calls the Windows function MessageBox, which takes an unsigned integer argument.

    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
    

    You can test the function messageThroughWindows with the following code.

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

    Warning

    The UInteger, ULong, UShort, and SByte data types are not part of the Common Language Specification (CLS), so CLS-compliant code cannot consume a component that uses them.

    Security noteSecurity Note

    Making a call to unmanaged code, such as the Windows application programming interface (API), exposes your code to potential security risks.

    Security noteSecurity Note

    Calling the Windows API requires unmanaged code permission, which might affect its execution in partial-trust situations. For more information, see SecurityPermission and Code Access Permissions.

See Also

Tasks

How to: Optimize Storage of Positive Integers With Unsigned Types (Visual Basic)

Walkthrough: Calling Windows APIs (Visual Basic)

Reference

Data Type Summary (Visual Basic)

Integer Data Type (Visual Basic)

UInteger Data Type

Declare Statement