Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.

This is a special case of calling a foreign language function where the focus is close to the ABI level and not at the normal API level.



Related task

 OpenGL -- OpenGL is usually maintained as a shared library.


#INCLUDE "Win32API.inc"
 
FUNCTION PBMAIN () AS LONG
    DIM hWnd AS LONG
    DIM msg AS ASCIIZ * 14, titl AS ASCIIZ * 8
 
    hWnd = LoadLibrary ("user32")
    msg = "Hello, world!"
    titl = "Example"
    IF ISTRUE (hWnd) THEN
        funcAddr& = GetProcAddress (hWnd, "MessageBoxA")
        IF ISTRUE (funcAddr&) THEN
            ASM push 0&
            tAdr& = VARPTR(titl)
            ASM push tAdr&
            mAdr& = VARPTR(msg)
            ASM push mAdr&
            ASM push 0&
            CALL DWORD funcAddr&
        ELSE
            GOTO epicFail
        END IF
    ELSE
        GOTO epicFail
    END IF
 
    GOTO getMeOuttaHere
 
epicFail:
    MSGBOX msg, , titl
 
getMeOuttaHere:
    IF ISTRUE(hWnd) THEN
        tmp& = FreeLibrary (hWnd)
        IF ISFALSE(tmp&) THEN MSGBOX "Error freeing library... [shrug]"
    END IF
END FUNCTION