Image of Navigational Map linked to Home / Contents / Search Some Porting tips for VB5

by Dave Thompson - GUI Computing
Image of Line Break

The are 2 main things to worry about when porting code from 16 bit VB programs to 32 bit VB programs. They are:

1. Converting VBXs to 32bit OCXs;

2. Converting API calls to Win32.

Converting to OCX

VB5 will automatically upgrade a VBX to the equivalent OCX. An entry in the VB.INI file tells VB which VBX corresponds to which OCX. Most custom controls will add this entry to the VB.INI file when they are installed. However, sometimes you may have to do this yourself. The entry will look something like this:

[VBX Conversions32]
The easiest way to obtain this entry is to add the custom control to your project. VB will add a line in the project file that looks something like this:
Object={2037E3AD-18D6-101C-8158-221E4B551F8E}#5.0#0; VSOCX32.OCX

This line can then be copied and pasted into the VB.INI file. Next time you fire up your old VB project in VB5, you will be prompted to upgrade the VBX to the new OCX. You may still have some (or a lot of!) work to do depending on how compatible the new control is with the old one. If VB cannot upgrade a VBX, it will replace it with a picture box, which is generally not what you want.

Converting API calls

Having done all that, you're now ready to port your 16 bit API calls to Win32. How hard this is depends on the particular function you're converting. In this case, the GetAsyncKeyState function is fairly easy to convert. If you're using VB4-16, then you can use the pre-processor to define the Win16 and Win32 API calls in the same source code. For Example :

Const VK_ESCAPE = &H1B
#If Win32 Then
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Declare Function GetAsyncKeyState Lib "User" (ByVal vKey As Integer) As Integer
#End If

This enables you to write the EscapePressed function, which returns true if the escape key has been pressed since the last call to the function. This is handy if you want to have a user cancel some operation by hitting the escape key. In this case, the KeyDown event won't work because it goes into the Windows message queue, and you won't see the message until the operation has finished.

Function EscapePressed() As Boolean

    EscapePressed = IIf(GetAsyncKeyState(VK_ESCAPE), True, False)
End Function

It is necessary to initialise this function by calling it once first an ignoring the result. This is because the escape key may have been pressed before your processing starts, and the function will return true the next time it is called.

Finally, it is always a good idea to write a wrapper function, such as EscapePressed, when dealing with conditional code. This allows you to write the rest of the program independent of which version of Windows you are targetting. Yes, I know we're talking about VB5 and 32 bits now. But that's now. Wrapping is still a good investment for the future.

Oh, and one last thing.... before you start any of this, make sure you've saved your old source as text... VB5 won't read it any other way.

Written by: Dave Thompson
May '97

Image of Arrow linked to Previous Article Image of Arrow linked to Next Article
Image of Line Break