AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

Converting your applications from VB6 to VB.NET

Windows APIs

Many APIs can be used exactly as they were in Visual Basic 6.0, with the caveat that you have to adjust your data types accordingly. The Visual Basic 6.0 Long datatype is now the Visual Basic.NET Integer datatype, and the Visual Basic 6.0 Integer datatype is now the Visual Basic.NET Short datatype. During the upgrade, these changes are made for you, and simple APIs work exactly the same as they did in Visual Basic 6.0. 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

changes 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 numeric datatype upgrades, Visual Basic 6.0 had a fixed-length string data type which is not supported in Visual Basic.NET, and which is upgraded to a fixed-length string wrapper class. In many cases in Visual Basic 6.0 you can perform the same action using a normal string. 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

can be better written using a normal string explicitly set to length 25 instead of a fixed-length string:

 Dim Buffer As String
 Buffer = String$(25, " ")

This is upgraded to Visual Basic.NET as follows:

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 better handles passing strings to APIs, since you can optionally declare how you want strings to be passed using the ANSI and UNICODE keywords.

There are three cases where you may need to make some changes. The first is passing user-defined types that contain fixed-length strings or byte arrays to APIs. In Visual Basic.NET you may need to change your code, adding the MarshallAs attribute (from System.Runtime.InteropServices) to each fixed-length string or byte array in the user-defined type. The second case is using the As Any variable type in a Declare statement. This is not supported in Visual Basic.NET. Variables of type As Any were often used to pass a variable that was either a string or Null; you can replace this Visual Basic 6.0 usage by declaring two forms of the API, one with longs, one with strings. For example, the GetPrivateProfileString API has a parameter lpKeyName of type As Any:

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 remove the “As Any” by replacing the Declare with two versions; one that accepts a long, and one that accepts a 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

When you wish to pass the value Null to the API, you use the GetPrivateProfileStringNullKey version. Doing it this way means that the function upgrades to Visual Basic.NET.

The final area where you may need to make some changes is if you are using APIs that perform thread creation, Windows subclassing, message queue hooking, and so on. Some of these functions will cause a run-time error in Visual Basic.NET. Many of these APIs have equivalents in Visual Basic.NET or the .NET Framework. You will have to fix these on a case-by-case basis.

 

Read the entire article here.
Return to Tutorials Main

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/