March 4, 2021
Hot Topics:

Windows API Tutorial - Part One

  • By Karl Moore
  • Send Email »
  • More Articles »

For your first API call, we're going to check out a highly cool yet cunningly simple function. It's called 'GetTickCount' and is a lovely little procedure that hides within kernel32.dll. It returns the number of milliseconds since you started Windows.

  • Open Visual Basic and create a new Standard Exe
  • Insert a Command Button on Form1
  • Add a Module to the project by clicking on Project, Add Module, Open

You typically throw all your API code into a module, making it accessible throughout your application.

Type the following into the module:

Declare Function GetTickCount Lib "kernel32.dll" _Alias "GetTickCount" () As Long

Type the following behind the Form1 Command Button:

Private Sub Command1_Click()Dim lngTickCount As Long

lngTickCount = GetTickCount

Call MsgBox("You have been using your computer for:" & _vbNewLine & " * " & CStr(lngTickCount) & _" milliseconds, or" & _vbNewLine & " * " & CStr(Round(lngTickCount / 1000)) & _" seconds, or" & _vbNewLine & " * " & CStr(Round((lngTickCount / 1000) / 60)) & _" minutes", vbInformation)' Note to VB5 Users - the Round() function here simply' rids of any numbers after the decimal point. It is' only available in versions 6+. Therefore, VB5 users' should remove the 'Round' word

End Sub

Press F5 to run the application and try hitting the Command Button

See what happens? My message box says "You have been using your computer for: 2564012 milliseconds, or 2564 seconds, or 43 minutes". Now is that megacool or what?

Sure, it's about as useful as a solar-powered lamp, but it's something you wouldn't be able to do purely in Visual Basic -- without accessing those external DLLs.

Let's explain what we did here. First off, we declared our API call in the module. It went something like this:

Declare Function GetTickCount Lib "kernel32.dll" _Alias "GetTickCount" () As Long

Here, we're saying we want to declare a function called GetTickCount (or whatever name you want to give it). The workings of this function reside in the "library" of groovy chocolate chip code within "kernel32.dll". The piece of code we want to run inside the DLL has an Alias (name) of GetTickCount (basically, the function name in kernel32.dll). Right at the end, we're telling it the function returns a long data type (the milliseconds).

Top Tip: You may notice that when you type in this code, Visual Basic shortens it to "Declare Function GetTickCount Lib "kernel32.dll" () As Long". That's OK -- when VB notices that both the name of the declaration and the name of the code inside in the DLL are the same -- it removes the Alias bit.

So that's how you declare the GetTickCount API call. How do you 'run' it? Well, you just do something like this:

MsgBox GetTickCount

In other words, you access it just like any normal Visual Basic function!

The code behind our Command Button takes the millisecond information from GetTickCount, then does a little division to figure out the number of seconds and minutes since Windows loaded.

Note: You can download our GetTickCount example by clicking here.

Don't get me wrong -- not all API calls are as simple as this blighter -- but it's a start!

In general, you declare an API call like this:

[Public/Private] Declare Sub/Function name Lib "dllname" _[Alias "aliasname"] [(argumentlist)] [As type]

Looks pretty weird doesn't it? Don't worry about it all just yet -- simply use it as a basic template for declaring API calls. Stuff in [square brackets] means that bit is optional, whilst The/Slash means you should choose OptionOne/OrTwo. The argumentlist is a potential list of arguments that may or may not exist.

That's all well and good, but how did I find out about that GetTickCount function? In the next section, we'll answer this question and more... read on, good sir!

Page 3 of 6

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date