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.


' FB 1.05.0 Win64
 
' Attempt to call Beep function in Win32 API
Dim As Any Ptr library = DyLibLoad("kernel32.dll") '' load dll
 
If library = 0 Then
  Print "Unable to load kernel32.dll - calling built in Beep function instead"
  Beep : Beep : Beep 
Else
  Dim beep_ As Function (ByVal As ULong, ByVal As ULong) As Long  '' declare function pointer
  beep_ = DyLibSymbol(library, "Beep")
  If beep_ = 0 Then
    Print "Unable to retrieve Beep function from kernel32.dll - calling built in Beep function instead"
    Beep : Beep : Beep
  Else 
    For i As Integer =  1 To 3 : beep_(1000, 250) : Next 
  End If
  DyLibFree(library) '' unload library
End If
 
Print
Print "Press any key to quit"
Sleep