27.2 API函数的类型
Posted VB.Net
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了27.2 API函数的类型相关的知识,希望对你有一定的参考价值。
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
API函数最开始是为了方便C语言开发者使用,所以现在MSDN也只有C语言的原型和对应说明。
以下转换是我的经验,大家需要根据实际情况设置对应的类型。
C类型 | Vb6类型 | Vb.Net类型 |
BOOL | Boolean | Boolean |
Char | String | Char |
BYTE | Byte | Byte |
WORD | Integer | Short,Int16 |
DWORD | Long | Integer,Int32 |
LONG | Long | Integer,Int32 |
UINT | Long | Integer,Int32 |
HANDLE | Integer | IntPtr(注1) |
HDC | Integer | IntPtr |
P开始的类型(注2) | Integer | IntPtr |
LP开始的类型(注2) | Long | IntPtr |
LPTSTR | String | String |
结构 | 类型(Type) | 结构,IntPtr,Byte() |
注1:根据操作系统位数,32位操作系统下IntPtr可视为Int32,64位操作系统下IntPtr可视为Int64。
注2:C语言中P和LP开头的类型通常都是指针。
上面的列表只是类型变化的一般情况,特殊情况特殊分析。
以GetSystemDirectory这个Api函数为例:
GetSystemDirectory:获得系统目录的路径,系统目录包含了系统文件(比如动态链接库文件和驱动等),通常是C:\\Windows\\System或者C:\\Windows\\System32。
C原型(参看MSDN):
UINT WINAPI GetSystemDirectory( _Out_ LPTSTR lpBuffer, _In_ UINT uSize );
Vb6中的声明:
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
VB.Net中通常的声明:
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
其实可以有更多的声明方式,在【例 27A.2】中将用4中不同的声明来使用GetSystemDirectory这个Api函数。
【例 27A.2】【项目:code27A-002】获得系统目录路径。
Const Max_Path As Integer = 260
Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As IntPtr, ByVal nSize As Integer) As Integer
Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer() As Byte, ByVal nSize As Integer) As Integer
Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As System.Text.StringBuilder, ByVal nSize As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim systemPath As String = New String(" "c, 260)
Dim returnValue As Integer
returnValue = GetSystemDirectory(systemPath, Max_Path)
TextBox1.Text &= vbCrLf & "String:" & vbCrLf & systemPath.Substring(0, returnValue)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim pBuffer As IntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(Max_Path)
Dim returnValue As Integer
returnValue = GetSystemDirectory(pBuffer, Max_Path)
Dim systemPath As String
systemPath = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pBuffer)
TextBox1.Text &= vbCrLf & "IntPtr:" & vbCrLf & systemPath
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim btePath(Max_Path - 1) As Byte
Dim returnValue As Integer
returnValue = GetSystemDirectory(btePath, Max_Path)
Dim systemPath As String = System.Text.Encoding.ASCII.GetString(btePath)
TextBox1.Text &= vbCrLf & "Byte():" & vbCrLf & systemPath.Substring(0, returnValue)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim systemPath As New System.Text.StringBuilder(Max_Path)
Dim returnValue As Integer
returnValue = GetSystemDirectory(systemPath, Max_Path)
TextBox1.Text &= vbCrLf & "StringBuilder:" & vbCrLf & systemPath.ToString
End Sub
运行结果如下图所示:
图27A-2 多种Api声明获取系统目录路径
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
以上是关于27.2 API函数的类型的主要内容,如果未能解决你的问题,请参考以下文章