Windows API

Declaring Windows API

The problem with these files is knowing how to access them, and what parameters they require. When you install Visual Basic, it comes with a handy program called an API Viewer. This lists all the functions provided by windows DLLs. These functions are called Windows API functions. The program does not access the DLLs but has a file with all the parameters , and in which DLL the function is located. However, as there are so many functions it is hard to find the one you want. So here I have listed the main DLLs and their parameters. Some of them are repeated, however I have listed them as sometimes on function does different jobs (restart, log off etc are all done by the same function).

To use a Windows API function in VB, you need to declare it using the Declare Function or Declare Sub statement. They take the following syntax:

Scope Declare Function Name Lib DLLName Alias AliasName (Parameters) As ReturnType

Scope Declare Sub Name Lib DLLName Alias AliasName (Parameters)

Where Scope is the scope of the DLL. Name is the name to be used to identify the function. DLLName is the DLL that the function is found in. AliasName is the actual name of the function in the DLL file. Parameters is the Parameters of the DLL function, and ReturnType is the data type of return value.

In many declarations, you will notice that there are many variables that begin with lp and then the name ie lpClassName. This generally means that there are various constants that can be used for this parameter. To find these, you need to select Constants from the API Type drop down. Next, you need to search for likely constant names. For example, the constants used for the GetFileAttributes function all begin with FILE_ATTRIBUTE. You just have to use your common sense, as there is sometimes now obvious name. Once you have found the constant, click Add to add it to your selection.

Another thing you may notice is that in a parameter, apart from the normal data typs such as Long, String etc, there are types you do not know. These are usually in CAPITALS. More often that not, these are custom Types, which can be found in the Types section of the API Viewer. So, select Types from the API Type drop down and enter the name of the type. You should find an entry under that name. Click Add to add it to your selection.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous