Home   .......

Using Windows API in VB Tutorial

API Declaration

Products  
eXamples  
Files  
e-Mail Us  
 

 

As said in What's API, functions are declared in Dlls located in the Windows System directory. You can type in the declaration of an API just as you do with any other function exported from a Dll, but VB has provided an easier way to do it. It is called API Text Viewer.

To have some API declared in your project, just launch API Text Viewer, open Win32Api.txt (or .MDB if you have converted it into a database to speed it up), choose Declares, find the function, click Add and then Copy. Go to your project and paste it in. Do the same to have a predefined constant or type.

There are few problems you may face:

Suppose you want to declare a function in your form module. You paste the declaration and run the program. VB says Compile Error, ...and Declare statements not allowed as Public members of ... . Sounds bad, but it isn't, all you need to do is add Private in front of the declaration (like Private Declare Function ...). Do not forget, though, that the function will be visible only within this form module.

In some cases, you may get the message Ambiguous name detected by VB. It means you have two functions, constants or whatsoever sharing one name. As most of the functions (maybe all of them, I haven't checked that) are aliased, which means they are given different names, and not their original using Alias clause, you may simply change the name of the function and it will still work.

You may read the Declare statement help topic of VB's for description of Alias.

 
Copyright (c) 1998, Billy&George Software and Peter Dimitrov
Revised March 2000