1.Windows的API有兩個版本的,比如ANSI的FindWindowA(),Unicode的FindWindowW()

鄰水網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
2.兩種聲明的方式
DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow") _
Shared Function FindWindow(ByVal className As String, ByVal windowName As String) As IntPtr
End Function
Declare Auto Function FindWindow Lib "user32.dll" Alias "FindWindow" (ByVal className As String, ByVal windowName As String) As IntPtr
Private Sub Form_Load()
Me.Show
Text1.SetFocus
End Sub
注:要使得窗體先顯示出來,再設(shè)置Text1的焦點,不然會出錯的。
與vb6.0的語法一樣啊
Private Declare Function 函數(shù)名 Lib "庫名" Alias "別名" (形參1 As 類型,形參2 As 類型,形參3 As 類型,形參4 As 類型,形參5 As 類型) As 返回值類型
(1).使用DllImport特征類來申明Windows API函數(shù):
下面是在Visual Basic .Net中使用DllImport特征類申明二個Windows API函數(shù)的具體示例:
'函數(shù)ExtractIcon,其功能是是從指定文件的指定位置導(dǎo)出圖標(biāo)的Windows句柄。
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" , EntryPoint := "ExtractIcon" ) > _
Public Function _
ExtractIcon ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function
'函數(shù)Icon_Num,其功能是獲得指定文件中的圖標(biāo)數(shù)目
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" , EntryPoint := "ExtractIcon" ) > _
Public Function _
Icon_Num ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
End Function
在使用DllImport特征類申明Windows API函數(shù)時,如果申明的函數(shù)名稱和函數(shù)的入口點相同,則可以在申明Windows API函數(shù)時,省略定義函數(shù)入口點對應(yīng)的代碼,即EntryPoint對象字段對應(yīng)的代碼,這樣聲明ExtractIcon函數(shù)的代碼也可以簡化為如下所示:
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" ) > _
Public Function _
ExtractIcon ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function
(2).使用“Declare”語句來申明Windows API函數(shù):
使用“Declare”語句的確比使用DllImport特征類要簡單了許多,下面是在Visual Basic .Net中使用“Declare”語句來聲明上述二個Windows API函數(shù)的具體方法:
Declare Auto Function ExtractIcon Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
'聲明ExtractIcon函數(shù)
Declare Auto Function Icon_Num Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
'聲明Icon_Num函數(shù)
在Visual Basic .Net中聲明Windows API函數(shù)時,“Declare”語句中Alias關(guān)鍵字的作用相當(dāng)于使用DllImport特征類中的EntryPoint對象字段。同樣在使用“Declare”語句聲明Windows API函數(shù)時,如果聲明的函數(shù)和函數(shù)的入口點相同,也可以省略Alias關(guān)鍵字對應(yīng)的代碼,所以ExtractIcon函數(shù)也可以簡化為如下:
Declare Auto Function ExtractIcon Lib "Shell32.dll" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
名稱欄目:vb.net調(diào)用api vbnet調(diào)用api
文章來源:http://www.chinadenli.net/article30/dddjsso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、自適應(yīng)網(wǎng)站、小程序開發(fā)、微信公眾號、網(wǎng)站策劃、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)