Extending Classic (6.0) Visual Basic – Part 1
Visual Basic 6.0 just doesn’t want to disappear. Why ? Likely many have written vital applications using VB 6.0 and Visual Basic dot.net is so different that it is not a viable option. If you have thousands of lines of legacy VB code which can not be upgraded to dot.net easily, then what is a developer to do ?
It is time to learn the WIN32 API
While it is true that many classic VB developers dabbled with the WIN32 API over the years, maybe it is time to dig deeper into the WIN32 API now, so one can tap into its raw power so one can extend ones VB application. It is not simply a matter of just using a WIN32 API function call once in awhile, but by learning how the WIN32 really works so you can find ways to leverage the WIN32 in ways you may never have thought of before.
One of the problems with classic Visual Basic is that it shields you too much from the WIN32 API so it is harder to appreciate why the WIN32 does things the way it does. The low level WIN32 is not object oriented, but is procedurally based (at least the core API). There are good reasons for why it works the way it does and it is actually very efficient. So to better appreciate how to extend classic Visual Basic, let’s start by learning a little more about the low level WIN32 API and ways to integrate it into our VB apps better.
Start by having the right tools
For a long time classic Visual Basic programmer to be able to get the most out of the WIN32 API, you need the right tools. The goal of this series of articles is not to simply teach a few API function calls which you can add to your VB app, but it is to show you how to build your own addons to Visual Basic which you can use in all your VB apps. Now the first thing many VB’ers might think is that such addons would be OCX controls and one needs to learn C to create these. Actually I want to stay completely away from OCX controls and instead concentrate on writing DLL’s (Dynamic Link Libraries). DLL’s have the benefit of not requiring any special installation (no need to register with OS).
Another goal is to progressively eliminate the need for any third party OCX controls. One problem with classic Visual Basic was that so many developers extended it via third party custom controls which now are no longer supported. If we can progressively eliminate these and try to get down to just the main VB runtime, then our apps will be more easy to manage.
One other thing that I find is a weakness in classic VB is that even many system controls found in Windows are implemented via OCX controls. Why they weren’t supported in the main runtime is a mystery too me. Even these OCX controls need to be eliminated from our VB apps. So where do we start ?
If a programmer really wants to do a lot of low level WIN32 coding, it would seem that he/she should learn C (not C++). But for a long time Visual Basic programmer that is counter productive, since they are so used to BASIC. So is there an alternative to C for VB programmers ? Yes, there is. While I could point to a number of indie BASIC language compilers for Windows, such as Free basic or Pure Basic, the best BASIC language IMO is Powerbasic. It is not free, but it is low cost and well worth the price. Why ?
Powerbasic has been around the longest. It dates back to the DOS days and many may not realize this, but Powerbasic used to be called “Turbo Basic”. Yes, it was one of the few BASIC compilers which could compete with Microsoft’s famous Quick Basic 4.0 (and PDS 7.1). What you may not know is that Borland International did not create Turbo Basic. They purchased the rights to it from a developer named Bob Zale. When Borland got out of the programming tool market, they sold the rights for Turbo Basic back to Bob Zale who renamed it Powerbasic. Today Powerbasic is owned by Drake Software.
Unlike classic Visual Basic which was not originally developed as a native compiler (it was a Pcode compiler), Powerbasic was originally developed as a Windows native code compiler from the beginning. It used Microsoft BASIC’s as its roots, but added C like features to make it more compatible with other languages, as well as Windows. Powerbasic gives you features crucial to working with the WIN32 , as well as with other languages. One simple example is its support for multiple procedure/function calling conventions. Windows uses the “Standard Calling Convention” or STDCALL. This is the default calling convention of Powerbasic. But it offers more calling conventions so you can interface with other programming languages. The calling conventions available are:
- Standard Calling Convention (STDCALL) (native to Windows)
- C Calling Convention
- BASIC/Pascal Calling Convention
You can also define subroutines/functions to be exported (use by external programs). Powerbasic also provides many low level features such as inline assembler, pointers, callback functions and many data types crucial for calling WIN32 API’s. Being able to write WIN32 code similar to how C programmers do, but with a BASIC syntax is important for Visual Basic programmers because they are a already familiar with it.
The next important tool is to download the Windows SDK documentation. You can download it here:
https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk
I do recommend downloading and installing different versions of the SDK. Download and install the latest SDK, but I strongly recommend downloading the Windows 7 SDK:
https://developer.microsoft.com/en-us/windows/downloads/sdk-archive
The older SDK documents are easier to find your way around and it also eliminates many of the later “bleeding” edge API’s of Windows 8 and 10. Most of what you need will be found in Windows 7 (actually as far back as Windows XP).
Next, here is a Freeware utility called PB WinSpy, written by Borje Hagsten:
http:/cwsof.com/download/PBWinSpy.zip
This is a great utility for use with Powerbasic, but it is even useful for all programmers. It can “Spy” into any program and show you what Windows classes are used for the entire visible user interface and even generate WIN32 code to recreate the same user interface (compilable using Powerbasic). Just use it to check out your Visual Basic app. You will be surprised to see that VB apps don’t directly use system window classes, but instead use a proprietary set of Window classes specific to Visual Basic (like superclasses of the system classes). For example the system BUTTON class is not what Visual Basic uses. Using PBWinSpy you will see that the window class is:
ThunderCommandButton
Visual Basic uses this Thunder…. name syntax for its controls. Now let’s look at a simple sample VB 6.0 app:
Now let’s use the PBWinSpy utility to capture information about the form and then generate some WIN32 code (compilable using Powerbasic) from it:
Now let’s look at the code generated by PBWinSpy:
Note: The above is only a picture! You can download the actual code here:
http://cwsof.com/download/codesample.txt
Yes, real WIN32 code in minutes. While you may not use the generated code in your addons for Visual Basic, it is a great learning tool. Notice the PBWinSpy utility tried to convert the Visual Basic Thunder… classes into the proper system window classes and gets some of them right, but not all of them. That’s one problem with Visual Basic’s use of proprietary window class names. PBWinSpy just gets you started. Now if the sample app was not written in Visual Basic, but some other language which does use the native system window classes, then PBWinSpy would have generated perfectly working code. The window class names above: FRAME, OPTION, CHECKBOX don’t exist as individual window classes, but are actually all part of the BUTTON class, so those lines would have to be modified.
The last tool or resource is the Powerbasic online forums:
There are not many WIN32 programming forums around today, but the one for Powerbasic users is IMO one of the best resources around for learning about low level WIN32 programming. WIN32 coding is becoming a lost art today and any resource which can help you learn it is valuable. Now you may ask, “Is the WIN32 even viable today and worth learning ?”
Absolutely , yes!
I have been a WIN32 programmer for the last 20 years and everything I write works on Windows XP to Windows 10 without problems. As long as you know which API’s to use, you can target multiple versions of Windows from XP to Windows 10. Most of what a programmer needs today is in Windows XP. Do you doubt that ? Why not download a simple test app I wrote for testing whether the API’s I use work properly on different versions of Windows:
http://cwsof.com/download/testwin8.zip
The app uses a GUI framework I wrote for Windows (using Powerbasic) which should work on any version of Windows from XP to Windows 10. The entire framework was written using the WIN32 API (compiled using Powerbasic 9.0).
While I have been a Powerbasic WIN32 programmer for the last 20 years, I decided to revive working with classic Visual Basic for this series of articles. The last version of Visual Basic I actually did any programming with was VB 5.0 Professional. For this article series (and some other projects) I searched for (and found) a New “Old Stock” copy of Visual Studio 6.0 (in a sealed original package) and purchased it. It was a rare find. I want to see how far I can push classic Visual Basic by extending it using Powerbasic and the WIN32. If you have any suggestions of what you would like to see in future articles, feel free to post them below (on Code Project only).
The last thing I would like to mention is this. The efforts to learn the WIN32 API are not a waste of time on legacy code. The WIN32 is just as viable today as it was years ago. What do you think dot.net is built upon ? Yes, the WIN32. Consider the WIN32 as the “Assembler” language of Windows today. Just like true assembler was the low level coding method in the DOS days, today the WIN32 is the assembler of Windows. Any DLL’s you write using Powerbasic and the WIN32 can also be used with other unmanaged programming languages.