Click here to Skip to main content
15,885,119 members
Articles / Desktop Programming / Win32

Extending Classic (6.0) Visual Basic – Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
4 Sep 2019CPOL9 min read 11.6K   14   8
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.

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:

https://forum.powerbasic.com/

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.

This article was originally posted at http://cwsof.com/blog?p=1031

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Computer Workshop
United States United States
Chris Boss is the owner (and programmer) of a small software development business in rural Virginia, called the Computer Workshop. For the last ten years or so he has been developing tools for use by Powerbasic programmers (see: http://powerbasic.com ). His main product called EZGUI (Easy GUI) is a high level GUI engine with Visual Designer and code generator. It is in its fifth generation now. He is an experienced Windows API programmer (more low level) and has experience in writing GUI engines (forms/controls), drag and drop Visual Designers, Graphics engines (printing and to the screen) and one of his favorites is a Sprite engine (2D animated movable images). His current project is version 5.0 of his main product EZGUI, adding such features as multi-monitor support, component engine, custom control engine, superclass engine and the latest project a 3D OpenGL based custom control. One of the goals he has is to push the limits of Windows software development, while making it easy, fast execution speed, small footprint (size of executables) and code reusability while providing a more graphic experience in user interfaces, while still being able to write software which can fit on a floppy disk (small footprint), use minimal amount of memory and able to run on multiple versions of Windows from 95 to Win8.

Comments and Discussions

 
PraiseWin API can be used inside VB6 also Pin
Member 1140014317-Dec-19 9:44
Member 1140014317-Dec-19 9:44 
GeneralRe: Win API can be used inside VB6 also Pin
Chris Boss5-Jan-20 9:39
professionalChris Boss5-Jan-20 9:39 
Where Powerbasic goes beyond VB 6 is many low level features it has. Things like inline assembler, pointers, richer data types, calling functions via pointers and more.

I have done a lot of work with DIB sections writing low level image filters using Powerbasic. OpenGL is likely easier to work with in Powerbasic than in VB.

Lastly raw performance will likely be a real benefit in using Powerbasic. Even if a task can be done in VB, I would venture to say that porting it to a Powerbasic DLL will give you much improved performance. So when speed matters, then Powerbasic can make a big difference.

Using Powerbasic with VB 6 gives you the best of both worlds. VB gives you an easier IDE to work in and drag and drop design, whereas Powerbasic can add to it the raw performance and low level features not readily available in VB. Powerbasic has similar performance to writing code on C, but you get the BASIC syntax you are used to.
QuestionActivex Controller e DLL . Pin
Member 125267736-Sep-19 23:30
Member 125267736-Sep-19 23:30 
AnswerRe: Activex Controller e DLL . Pin
Chris Boss7-Sep-19 8:50
professionalChris Boss7-Sep-19 8:50 
GeneralRe: Activex Controller e DLL . Pin
Member 125267735-Jan-20 9:12
Member 125267735-Jan-20 9:12 
QuestionThank you Chris for a useful article. Pin
JohnBrock6-Sep-19 8:01
JohnBrock6-Sep-19 8:01 
QuestionI agree! Pin
davercadman6-Sep-19 5:53
davercadman6-Sep-19 5:53 
QuestionNice Article! Pin
Member 142066375-Sep-19 23:55
Member 142066375-Sep-19 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.