# 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:

1. Make sure that you really need the 64-bit NAG Library.
2. 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 vb6_headers or vba7-64_headers (depending on the version of the Library installed) subdirectories of the NAG Library installation directory.
3. 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:

1. Subroutines (and functions) must use the Ptrsafe keyword.
2. The name of the library (after the Lib keyword) must be changed to point to the 64-bit DLL.
3. Variables indicating an external (user supplied) subroutine or function (so fcn, output and g in example above) must use the longPtr variable type.
4. The "hidden" variable that holds the length of string variables (so relabsLength in the example above) moves to the end of the variable list and must use the LongLong variable type.

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 n and ifail in the example above) must use the LongLong type. This change must be made in addition to the four described above.