Calling C Numeric Library from Visual Basic and/or Excel
IMSL Library Insight:
Since Visual Basic has become the second most popular
programming language behind C, there has been an increased interest
in programmers and project managers to call third party technical
subroutine libraries from Visual Basic. Visual Basic now comes in
several formats including the standard Visual Basic IDE (VB), Visual
Basic for Applications (VBA), and VB Script. This makes Visual Basic
a language with a large presence in the application development
market and one that would benefit from the technical expertise currently
available in other libraries. The IMSL libraries, in particular
the IMSL C Numerical Library, fits neatly into the category of pre-built
technical subroutines that a VB programmer may have a need to access.
Generally, these type of routines are very technical and complex
making it undesirable to code them from scratch. Calling a standard
library of routines makes sense from a time standpoint and in terms of robustness.
Calling the IMSL C Numerical Libraries (CNL) from Visual Basic is largely a
matter of addressing the constraints each language places on the
mechanism for passing data in and out of subroutines. This includes
pointers, differences in the way arrays are stored in each language,
and other issues such as optional arguments. The optimal method
for addressing these issues is to use an intermediate library written
in the native language of the library. Here, data can be converted
to the proper types for each language. Optional arguments can also
be parsed and the correct calling sequence to the third party routine
can then be made. Results from the routine are passed back to the
calling program through the same parameter list. In our "VB calling
CNL" example, we employ a user-defined model class as the intermediate
storage mechanism for receiving data from the VB calling program
and for getting data and results from the called CNL function. You
should do other house cleaning chores such as checking the IMSL
error handler in the intermediate library as well. Errors should
be passed back to the VB program either as a global flag or as an
extra argument in your VB_to_C_routine parameter list.
Examples of
calling the CNL from both Visual Basic/VBA and from inside Excel
are available.
Visual Basic Examples (Zip file 152K)
Microsoft Excel Examples (Zip file 457K)