Calling an FTN95 DLL from Visual Basic

Calling an FTN95 DLL from Visual Basic

In this section:

An FTN95 DLL that is called by an executable that uses the STDCALL calling convention (e.g. one created using Win32 Visual Basic) must use F_STDCALL in the declaration of all exported subprograms. For example,

F_STDCALL FUNCTION F(X)
INTEGER F,X
F=X
END

Such a function could make calls to Windows API functions provided an interface is provided via, for example, "USE MSWIN" or "INCLUDE <windows.ins>". Alternatively an interface for each API function can be given explicitly. For example,

STDCALL SENDMESSAGE 'SendMessageA' (VAL,VAL,VAL,VAL):INTEGER*4

A call is automatically made to a DLL function called LIBMAIN when the DLL is loaded. If you do not provide a definition of LIBMAIN then SLINK will provide a default for you. LIBMAIN is used to initialise global data. It takes the following form.

F_STDCALL INTEGER FUNCTION LIBMAIN(hInst,ul,lpR)
INTEGER hInst,ul,lpR
!***** Initialise global data here
LIBMAIN=1
END

A simple SLINK script would take the form:

dll
lo f95.obj
exportall
file c:\windows\f95.dll

A Visual Basic program can use calls to the API functions LoadLibrary and FreeLibrary in order to ensure that the DLL does not unload whilst a Visual Basic form is loaded. Here is some sample code for this purpose.

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal s As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (h As Long) As Long
Dim hLibModule As Long

Private Sub Form_Load()
hLibModule = LoadLibrary("f95.dll")
End Sub

Private Sub Form_Unload(Cancel As Integer)
i& = FreeLibrary(hLibModule)
End Sub

The FTN95 DLL is assumed to reside in the Windows system directory.

 

 

Basket
Empty
 
Copyright © 1999-2021 Silverfrost Limited