Converting 32-bit Excel examples to call the 64-bit NAG Library
Calling the 64-bit NAG Library from Excel
In order to call the 64-bit NAG Library from Excel you need to do the following:
- Make sure that you really need the 64-bit NAG Library.
- Make sure that you are using the correct declarations for your version of the Library.
Each version of the NAG Library comes with its own set of header files to use with VBA which contain declarations for all routines in that library. You must use the correct declarations for your version of the Library and ensure that the types and order of the variables passed into the NAG Library routine match those specified in that declaration. The header files reside in either the
vba7-64_headers(depending on the version of the Library installed) subdirectories of the NAG Library installation directory.
- Write your VBA as normal.
Differences Between 32- and 64-bit Header Files
If you are writing a new set of VBA code, then the differences between the 32- and 64-bit header files are largely moot. However, when trying to convert code written to use the 32-bit NAG Library to call the 64-bit NAG Library it can be useful to know what the differences are. The two declarations below are both for the NAG Library routine D02BJF. The first is taken from the header files supplied with the 32-bit NAG Library and the second from those supplied with the 64-bit NAG Library.
32 bit DLL Declaration for D02BJF
Declare Sub D02BJF Lib "FLDLL244M_nag.dll" ( _ ByRef x As double, _ ByRef xend As double, _ ByRef n As long, _ ByRef y As double, _ ByVal fcn As Long, _ ByRef tol As double, _ ByVal relabs As string, ByVal relabsLength As long, _ ByVal output As Long, _ ByVal g As Long, _ ByRef w As double, _ ByRef ifail As long)
64 bit DLL Declaration for D02BJF
Declare Ptrsafe Sub D02BJF Lib "FLW6I24DC_nag.dll" ( _ ByRef x As double, _ ByRef xend As double, _ ByRef n As long, _ ByRef y As double, _ ByVal fcn As longPtr, _ ByRef tol As double, _ ByVal relabs As string, _ ByVal output As longPtr, _ ByVal g As longPtr, _ ByRef w As double, _ ByRef ifail As long, _ ByVal relabsLength As LongLong)
The differences between these two interfaces are that in the 64-bit interface:
- Subroutines (and functions) must use the
- The name of the library (after the
Libkeyword) must be changed to point to the 64-bit DLL.
- Variables indicating an external (user supplied) subroutine or function (so
gin example above) must use the
- The "hidden" variable that holds the length of string variables (so
relabsLengthin the example above) moves to the end of the variable list and must use the
Although not recommended for use with Excel, there is also a 64-bit NAG Library that uses 64-bit integers. When using this library all instances of
long in the declaration (so
ifail in the example above) must use the
LongLong type. This change must be made in addition to the four described above.