Declaring external functions and subroutines in Visual Basic
Any Fortran subroutine called from Visual Basic must reside in a dynamic link library (DLL). A DLL file is nothing more than a collection of functions and subroutines that can be loaded dynamically, when your Visual Basic program requests one of its components at run time. Because DLL procedures reside in files that are external to your Visual Basic application, you must specify where the procedures are located and identify the arguments with which they should be called. You provide this information with the Declare statement. Once you have declared a DLL procedure, you can use it in your code just like a native Visual Basic procedure.
To declare a DLL procedure, you add a Declare statement to the declarations section of the code window. If the procedure returns a value, write the declare as a function:
Declare Function publicname Lib "libname" [Alias "alias"] [([[ByVal] variable [As type] [,[ByVal] variable [As type]]...])] As Type
If a procedure does not return a value, write the declare as a Sub:
Declare Sub publicname Lib "libname" [Alias "alias"] [([[ByVal] variable [As type] [,[ByVal] variable [As type]]...])]
DLL procedures declared in standard modules are public by default and can be called from anywhere in your application. DLL procedures declared in any other type of module are private to that module, and you must identify them as such by preceding the declaration with the Private keyword.
Procedure names are case-sensitive in 32-bit versions of Visual Basic.
The Lib clause in the Declare statement tells Visual Basic where to find the .dll file that contains the procedure. When you're referencing one of the core Windows libraries (User32, Kernel32, or GDI32), you don't need to include the file name extension:
Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
For other DLLs, such as those created in Fortran, the Lib clause is a file specification that can include a path:
Declare Function lzCopy Lib "c:\windows\lzexpand.dll" (S As Integer, D As Integer) As Long
If you do not specify a path for libname, Visual Basic will search for the file in the following order:
Directory containing the .exe file
Current directory
Windows system directory (often but not necessarily \Windows\System or \winnt\System32)
Windows directory (often but not necessarily \Windows or \winnt)
Path environment variable
Occasionally, a DLL procedure has a name that is not a legal identifier. It might have an invalid character (such as a hyphen), or the name might be the same as a Visual Basic keyword (such as GetObject). When this is the case, use the Alias keyword to specify the illegal procedure name.
For example, some procedures in the operating environment DLLs begin with an underscore character. While you can use underscores in Visual Basic identifiers, you cannot begin an identifier with an underscore. To use one of these procedures, you first declare the function with a legal name, then use the Alias clause to reference the procedure's real name:
Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
In this example, lopen becomes the name of the procedure referred to in your Visual Basic procedures. The name _lopen is the name recognized in the DLL.
You can also use the Alias clause to change a procedure name whenever it's convenient. If you do substitute your own names for procedures (such as using WinDir for GetWindowsDirectoryA), make sure that you thoroughly document the changes so that your code can be maintained at a later date.
The Alias clause is also useful to adjust the name of functions and subroutines created with some Fortran compilers. Some compilers by default export DLL subroutines with all lowercase or uppercase names, while others prefix an underscore character ("_") and suffix "@nn" to the name of exported subroutines. You can use the Alias clause to make these procedures accessible to Visual Basic with a friendlier name. For example, let's say you have a subroutine in a Fortran DLL that has been exported with the name _ARCCOSINE@4, you can alias this name into a friendlier name with
Declare Sub ArcCosine Lib "MyFortran.dll" Alias "_ARCCOSINE@4" (ByRef X As Double) As Double
Passing arguments by value and by reference