首页 > 编程 > .NET > 正文

vb.Net编程简介之四

2024-07-10 13:02:31
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

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架构中有等价的函数。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表