Liberty BASIC Help Online

How to make API/DLL calls
 
Since making an API call is really the same as making a DLL call, the following applies to both.  In general, calling an API/DLL function works:
 
1) Open the desired DLL
2) Call the function or functions
3) Close the DLL
 
The desired DLL(s) can be opened when the program starts and closed when program execution is completed, or the DLL(s) can be opened just before calling the functions and then closed immediately afterward.
 
The following statements/functions are available for making API/DLL calls:
 
OPEN
OPEN "filename.dll" for dll as #handle
 
  - This form of OPEN opens a desired DLL so that a program can call functions in it.
 
Liberty BASIC 3 has Enhanced DLL handle resolution so that if a program hasn't opened certain default Windows DLLs, a reference to a like-named handle will still resolve to the desired DLL.  This will save on code to open and close DLLs.  The old way still works.  If a program OPENs a DLL, then it must be CLOSEd before the program ends.  If the default handles below are used, then a CLOSE command should not be issued.
 
Here are the default handles.
 
  #user32
  #kernel32
  #gdi32
  #winmm
  #shell32
  #comdlg32
  #comctl32
 
CALLDLL
CALLDLL #handle, "function", parm1 as type1[, parm2 as type2, ... ], result as returnType
 
  - This statement calls a named function in an OPENed DLL  The list of parameters gives information to the function that tells it how to perform its tasks.
 
See CallDLL.
 
HWND
HWND(#handle)
 
  - This function returns a window handle for the Liberty BASIC #handle
 
STRUCT
STRUCT name, field1 as type1 [, field2 as type2, ... ]
 
The STRUCT statement builds a specified structure that is required when making some kinds of API/DLL calls.  There is an example below that shows how to build a rectangle structure often used in making Windows API calls.
 
See also:  Using types with STRUCT and CALLDLL
 
Constants
 
Liberty BASIC has a library of defined Windows constants (_SW_HIDE being one).  This is equivalent to the definitions made in the windows.h file that comes with most C compilers.  The way to inline a Windows constant is to take the name of the constant and place an underscore in front of it.  "_SW_HIDE" is the same as the Windows constant "SW_HIDE."
 
If Liberty BASIC does not know the value of a Windows Constant, it will generate an undefined constant message when compiling program.  In this case, the numeric value of that constant must be determined from other sources
 
 
SAMPLE PROGRAM
Here is a short code sample (but not a complete program) using all of the above statements/functions:
 
NOMAINWIN
 
'create the structure winRect
    struct winRect, _
        orgX as long, _
        orgY as long, _
        cornerX as long, _
        cornerY as long
 
'define sizes
    openingWidth = 600
    expandedHeight = 400
 
'open USER32.DLL
    open "user32.dll" for dll as #user
 
    button #main.showMore, "Hide Me", [hide], UL, 10, 10
    open "An Example" for window as #main
    #main "trapclose [quit]"
 
'get the window handle for #main (a standard Liberty BASIC window)
    hMain = hwnd(#main)
 
'call the GetWindowRect API to load the window position/size into winRect
    calldll #user, "GetWindowRect", _
        hMain as long, _
        winRect as struct, _
        result as boolean
 
'extract the position information out of our struct
    xOrg = winRect.orgX.struct
    yOrg = winRect.orgY.struct
 
'call the MoveWindow API to resize the window to a predefined size
    calldll #user, "MoveWindow", _
        hMain as long, _
        xOrg as long, _
        yOrg as long, _
        openingWidth as long, _
        expandedHeight as long, _
        1 as boolean, _
        result as boolean
 
 
    WAIT
 
[hide]
'get the window handle to a button in our Liberty BASIC window
    hndl = hwnd(#main.showMore)
 
'call the ShowWindow API, passing _SW_HIDE to hide the button
    calldll #user, "ShowWindow", _
        hndl as long, _
        _SW_HIDE as long, _
        result as boolean
    WAIT
 
[quit]
    close #main
    'close USER.DLL
    close #user
    END
 
 
In the above example, the program calls three APIs from USER32.DLL, which is a standard Windows dynamic link library.
 
Here's what the code does:
 
1) creates the structure winRect
2) opens USER32.DLL
3) gets the window handle for #main (a standard Liberty BASIC window)
4) calls the GetWindowRect API to load the window position/size into winRect
5) extracts the position information from the struct
6) calls the MoveWindow API to resize the window to a predefined size
7) gets the window handle to a button in the Liberty BASIC window
8) calls the ShowWindow API, passing _SW_HIDE to hide the button
9) closes USER32.DLL
 
 
 
See also:  CALLDLLSTRUCT, Using Types with CALLDLL, What are APIs/DLLs?


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/