Alyce
Feb 23, 2007
- "Fixed formatting."
The ABCs of APIs Lesson 1
Table of Contents
What is an API Call?
An API call is aFunctions
What is a function? It is a block of code that takes in an argument or a list of arguments and returns a value. That sounds a little complicated, doesn't it? It's kind of like my bread machine. I put a list of ingredients into my bread machine, including flour, yeast and water. My bread machine takes these ingredients and manipulates them, then returns a loaf of fresh-baked bread to me.The list of ingredients can change. If I put in white flour, my bread machine returns white bread. If I use rye flour, it returns rye bread. If I use whole wheat flour, it returns wheat bread.
A function accepts a list of ingredients that we call arguments. (They can also be called parameters.) It manipulated these arguments and returns a value.
Liberty BASIC has many built-in functions. One of them is the
z = MAX(7,2)
print z
The arguments can be variables. The code using variables in place of literal numbers looks like this:
x = 7
y = 2
z = MAX(x,y)
print z
API calls are functions. We give the API function a list of arguments and it returns a value, which we store in a variable. API functions are similar to Liberty BASIC's built-in functions, but the syntax is a little different. API functions are accessed with the
CALLDLL
TheCALLDLL #user32,
That's not all, though! More information follows that statement, and we'll talk about each part in order.
Make note that
API Function Names
The next part of thecalldll #dll, "FunctionName",...
An actual API function called "IsIconic" looks like this:
CallDLL #user32, "IsIconic",...
API Arguments
Each API call has a set list of arguments that must be passed into the function. Each of these arguments gives the API function some information. The function evaluates this information. It returns a value based on the information in these arguments. Arguments can be literal numbers or strings, or they can be numeric or string variables. They *cannot* be array elements or expressions.Arguments are passed "as type". You can read more about types later in this tutorial.
Acceptable arguments:
calldll #dll, "FunctionName", argument1 as type1,...
calldll #dll, "FunctionName", 23 as type3,...
calldll #dll, "FunctionName", "Some Text" as type3,...
var$ = "A bit of text."
calldll #dll, "FunctionName", var$ as type4,...
Some unacceptable arguments:
calldll #dll, "FunctionName", array(1) as type1,...
calldll #dll, "FunctionName", 23 + 16 as type3,...
calldll #dll, "FunctionName", "Some Text" + "More Text" as type3,...
var$ = "A bit of text."
calldll #dll, "FunctionName", var$ + "hello" as type4,...
Some API functions require many arguments. A function with three arguments looks like this:
calldll #dll, "FunctionName", argument1 as type1, argument2 as type2, argument3 as type3,...
Handles
Many API calls require the Windows handle of a program window as one of the arguments.This Windows handle is a number of type ULONG. It is not the same as the "#handle" Liberty BASIC handle. We retrieve the Windows handle with theopen "A Window" for window as #1
'get the handle of the Liberty BASIC window
hWindow = HWND(#1)
Types
API functions need to know what kind of argument is being passed in. Arguments can be numbers, strings or structs. We will talk about numeric arguments in this tutorial.The most common numeric types are these. Types that hold numbers that are 4 bytes in size include ulong and long. Types that hold numbers that are 2 bytes in size include short, ushort, word and boolean. There are other types, but we won't discuss them in this tutorial.
A CALLDLL statement looks like this with actual types used in the arguments:
calldll #dll, "FunctionName", argument1 as ulong, argument2 as word, argument3 as long,...
Handle Types
In Liberty BASIC, handles must always be passed as either "ulong" type or "word" type.Return Values
API functions return a value, and we must include the "as type" for that value, just as we did for the list of arguments. There is a return type of "void" for functions that perform an action, but do not return a value. The numeric return types include ulong, long, short, word, ushort and boolean.Here is a generic call that includes the return value. The returned value is always the last part of the
calldll #dll, "FunctionName", argument1 as ulong, argument2 as word, result as boolean
Note that you cannot use a variable name for the return type that is called "return" because that is a Liberty BASIC statement. Variable names cannot be the same as statement or function names in the Liberty BASIC language. Liberty BASIC users often use a variable name of "result" to contain the value returned by the API function.
Our program often needs to know the value returned by the function so that it can perform the proper actions. Use "if/then" to manage program flow after an API call has been made.
Demo
Here is a small demonstration program. It opens a Liberty BASIC window. It retrieves the handle of the window with theThe handle is passed as type "ulong". The return type is "boolean". A boolean value is either nonzero (meaning it is true) or zero (meaning it is false.) If IsIconic returns a value of 0, the window is not minimized. If it returns any other value than 0, the window is minimized when the function is called.
open "A Window" for window as #1
#1 "trapclose [quit]"
'get the handle of the Liberty BASIC window
hWindow = HWND(#1)
'The IsIconic function determines whether the
'specified window is minimized (iconic).
CallDLL #user32, "IsIconic", hWindow as uLong, result As Boolean
'a result of 0 means that the window is NOT iconic
'a nonzero result means that the window IS iconic
if result = 0 then
print "Window is not minimized."
else
print "Window is minimized."
end if
wait
[quit] close #1 : end