vb.Net编程简介之四
2008-05-01 02:29
330 查看
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架构中有等价的函数。
大多数的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架构中有等价的函数。
相关文章推荐
- vb.net 多线程 编程 以及 通讯
- VB.NET编程调用讯雷下载文件
- VB6和VB.NET中对光标的编程
- vb.net 教程 3-11 窗体控件综合编程 2-2
- vb.net 教程 3-8 窗体编程 容器 2 TabControl 1
- VB.Net远程控制编程实例[鼠标钩子、 键盘钩子]
- C#图片框跟随鼠标移动_Asp_net教程__Net教程_Vb_net教程_Vc_net_C#编程.txt
- DirectUI Skin++界面解决方案 界面设计 界面开发 软件UI界面开发 软件UI界面美术设计 界面编程 界面教程 Skin VC VB C# .net pb delphi c builder GUI
- 编程 解压rar文件(适用C# VB.NET 和C++)
- vb.net 教程 3-2 窗体编程之窗体 3
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简介
- VB.NET编程中调用Windows API
- 在VB.NET编程中使用数组
- [实体框架编程] 第一章 ADO.NET实体框架简介(中)
- 利用VB.Net编程实现PC与掌上电脑PPC间的双向通信
- 关于嵌入窗体的编程(vb.net)
- VB.NET中网络编程的另一种方案----system.net中的HttpWebRequest类的使用
- VB.NET编程调用迅雷下载文件
- ASP.NET 3.5核心编程学习笔记(4):服务器控件简介
- VB.NET编程技巧——调用windows系统常用功能(原创文章)