Programming Windows APIs
Most API calls can be used as if they were in Visual Basic 6.0, because
The data type has changed. The long type in Visual Basic 6.0 is defined in Visual Basic.NET as an integer type. These definitions change automatically during the upgrade process, for example:
Private Declare Function getversion Lib "kernel32" () as
Long
Function Getver ()
Dim Ver as Long
Ver = GetVersion ()
MsgBox ("System Version is" & Ver)
End Function
Change to:
Private Declare Function getversion Lib "kernel32" () as
Integer
Function Getver ()
Dim Ver as Integer
Ver = GetVersion ()
MsgBox ("System Version is" & Ver)
End Function
In addition to the numeric type upgrades, Visual Basic 6.0 also supports fixed-length characters
String type, which is defined as a fixed-length string-compatible class after it is upgraded to Visual Basic.NET.
Therefore, it is best to use a generic string definition in Visual Basic 6.0 code, for example:
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
The above code appears with a fixed-length string, preferably changed to:
Dim Buffer as String
Buffer = string$ (25, "")
When you upgrade to Visual Basic.NET, you will be called the following:
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
In some cases, Visual Basic.NET can better control the passing of strings to
API calls, because you can define the way strings are passed through the ANSI and Unicode keywords.
There are three situations that require the most manual improvement of code.
1. Include fixed-length strings in custom data type definitions passed to API functions and
Array. In Visual Basic.NET you need to add MarshallAs attributes to each fixed-length string and array in a custom data type definition.
2. Use as any declaration in the definition. This kind of declaration is no longer being Basic.NET by visual
Hold, the variable defined as any is usually to pass a variable that is either a string or a null, in visual In Basic.NET, you can define two different types of APIs, one is a long type, one is a string type, and an API function GetPrivateProfileString as an example:
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
You can delete as any and instead define two different functions; a long number
Value, a receive string resin:
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
Use Getprivateprofilestringnullkey when you want to pass null values.
3, if you have a program such as the establishment of threads, Windows subclass (subclassing),
When API calls such as message hooks, these functions may produce run-time errors under Visual Basic.NET. Many of these APIs have equivalent functions in the Visual Basic.NET or the. NET architecture.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.