vb.Net編程簡介之四

來源:互聯網
上載者:User
編程 Windows APIs
    大多數的API調用可以象在Visual Basic 6.0中一樣使用,因為
資料類型發生了改變。在Visual Basic 6.0中的Long類型在Visual Basic.NET中定義為Integer類型。在升級過程中這些定義會自動改變,例如:

Private Declare Function GetVersion Lib "kernel32" () As
Long
Function GetVer()
    Dim Ver As Long
    Ver = GetVersion()
    MsgBox ("System Version is " & Ver)
End Function

改變為:

Private Declare Function GetVersion Lib "kernel32" () As
Integer        
Function GetVer()
    Dim Ver As Integer
    Ver = GetVersion()
    MsgBox("System Version is " & Ver)
End Function

    除了數字類型的升級外,Visual Basic 6.0還支援固定長度字元
串類型,該類型升級到Visual Basic.NET後會定義為固定長度字串相容類。

所以在Visual Basic 6.0代碼中最好使用通用字串定義,例如:

Private Declare Function GetUserName Lib "advapi32.dll"
Alias _
"GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As
Long) As Long
Function GetUser()
    Dim Ret As Long
    Dim UserName As String
    Dim Buffer As String * 25
    Ret = GetUserName(Buffer, 25)
    UserName = Left$(Buffer, InStr(Buffer, Chr(0)) - 1)
    MsgBox (UserName)
End Function

上面的代碼出現了固定長度字串,最好更改為:

    Dim Buffer As String
    Buffer = String$(25, " ")

升級到Visual Basic.NET後會稱為下面的樣子:

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As
Integer) As Integer
Function GetUser()
    Dim Ret As Integer
    Dim UserName As String
    Dim Buffer As String
    Buffer = New String(CChar(" "), 25)
    Ret = GetUserName(Buffer, 25)
    UserName = Left(Buffer, InStr(Buffer, Chr(0)) - 1)
    MsgBox(UserName)
End Function

    在有些情況下,Visual Basic.NET能夠更好的控制傳遞字串到
API調用,因為你可以通過ANSI 和UNICODE關鍵字定義字串傳遞的方式。

有三種情況需要對代碼最手工改進。
1、在傳遞給API函數的自訂資料類型定義中包含固定長度字串和
數組。在Visual Basic.NET中你需要對自訂資料類型定義中的每一個固定長度字串和數組添加MarshallAs 屬性。
2、在定義中使用As Any聲明。該種聲明不再被Visual Basic.NET支
持,變數定義為As Any通常是為了傳遞一個既可能是字串也可能是Null的變數,在Visual Basic.NET中,你可以定義兩個不同類型的API,一個為Long類型,一個為String類型,以API函數GetPrivateProfileString 為例:

Private Declare Function GetPrivateProfileString
Lib "kernel32" Alias
   "GetPrivateProfileStringA" (ByVal lpApplicationName As
String, ByVal
      lpKeyName As Any, ByVal lpDefault As String, ByVal
         lpReturnedString As String, ByVal nSize As Long,
ByVal
            lpFileName As String) As Long

你可以刪除As Any而代之以定義兩個不同的函數;一個接受Long數
值,一個接收String樹脂:

Private Declare Function GetPrivateProfileStringKey
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 GetPrivateProfileStringNullKey
Lib "kernel32"
   Alias "GetPrivateProfileStringA" (ByVal
lpApplicationName As String,
      ByVal lpKeyName As Long, ByVal lpDefault As String,
ByVal
         lpReturnedString As String, ByVal nSize As Long,
ByVal
            lpFileName As String) As Long

當你希望傳遞Null數值時,使用GetPrivateProfileStringNullKey。

3、如果你的程式中有諸如建立線程、Windows子類(subclassing),
訊息鉤子等API調用時,這些函數在Visual Basic.NET下可能會產生執行階段錯誤。很多這些API在Visual Basic.NET或者.NET架構中有等價的函數。

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。