如果您要取用具有不帶正負號整數類型成員的類別、模組或結構,您可以使用 Visual Basic 存取這些成員。
呼叫採用無符號型別的 Windows 函式
使用 Declare 語句 告訴 Visual Basic 哪個連結庫會保存函式、該連結庫中的名稱、其呼叫順序為何,以及如何在呼叫時轉換字串。
在
Declare
語句中,針對每個無正負號類型的參數,適當地使用UInteger
、ULong
、UShort
或Byte
。請參閱您所呼叫之 Windows 函式的檔,以尋找它所使用的常數名稱和值。 其中許多定義於 WinUser.h 檔案中。
在程序代碼中宣告必要的常數。 許多 Windows 常數都是 32 位無符號值,您應該宣告這些
As UInteger
。以一般方式呼叫 函式。 下列範例會呼叫 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
數據類型不是 Language Independence 和 Language-Independent Components (CLS) 的一部分,因此符合 CLS 規範的程式代碼無法使用使用這些類型的元件。這很重要
呼叫 Unmanaged 程式代碼,例如 Windows 應用程式開發介面 (API),會讓您的程式代碼面臨潛在的安全性風險。
這很重要
呼叫 Windows API 需要未受控程式碼的許可,這可能會在部分受信任的情況下影響其執行。 如需詳細資訊,請參閱 SecurityPermission 和 程式代碼訪問許可權。