Calling Functions in External DLLs

As illustrated in Objects, Methods, and Properties, one method to access functions in an external DLL (program) that supports Visual Basic (e.g., such as Microsoft Excel) is to select References from the Statistica Visual Basic Tools menu to load the respective libraries. The objects, functions, methods, etc. in that library are now accessible like all STATISTICA Visual Basic functions. You can also access "exported" functions or subroutines in other DLLs, even if they were not specifically designed to work with Visual Basic. For example, if you are an experienced C++ programmer, you may want to add some highly customized computational routines to your Statistica Visual Basic program. In a sense, this method of accessing "the outside world" allows you to add custom program options to Statistica; for example, you may have a proprietary complex algorithm, implemented in a complete program (which includes its own user interface), for evaluating stocks based on historical data. You could write a Statistica Visual Basic program to put a new option on the Statistics menu to execute this program and open your custom program when this option is chosen.

There are a number of things to consider when writing your own DLLs that are to be called by Statistica Visual Basic or when writing Statistica Visual Basic programs to access functions in such DLLs.

Declaration of functions and subroutines in the external DLL. SVB requires that the external DLL use the __stdcall calling conventions. This is true of all Win32 API calls (allowing them to be used directly from SVB), but when defining your own entry points in a custom DLL, care must be taken to make sure the export is defined correctly. Here are two prototypes of sample routines that could be called from SVB:

extern "C" int AFX_API_EXPORT WINAPI SampleFunction(int x);

extern "C" void AFX_API_EXPORT WINAPI SampleSubroutine(double q);

where:

extern "C"

means this name is not "decorated" with C++ type information.  Because of function overloading (a feature specific to C++), C++ compilers must "decorate" (or add extra unique characters) to function names internally to tell functions with the same names apart from each other.

AFX_API_EXPORT

is defined to __declspec(dllexport), which tells the compiler to export this name;

WINAPI

is defined to __stdcall, meaning use "standard calling conventions."

Calling external functions and subroutines. To use these routines in Statistica Visual Basic, you need to "declare" them in the declarations area of your SVB program (before the first function is defined). Refer to the SVB general documentation for a full explanation of the Declare statement and its options. For example:

Private Declare Sub SampleSubroutine Lib _
"C:\DLLTest\Debug\DLLTest" _

Alias
"_SampleSubroutine@4" (ByVal X As Integer)

 Private Declare Function SampleFunction Lib _
"C:\DLLTest\Debug\DLLTest" _

Alias
"_SampleFunction@4" (ByVal X As Integer) _
As Integer

Sub Main

Dim ii As Integer

Dim jj As Integer

jj = 1

ii = SampleFunction(jj)

SampleSubroutine(jj)

End Sub

This assumes that the DLL these routines are located in is DLLTest.dll, and that this DLL is located in the C:\DLLTest\Debug\ directory. Note that if the DLL was located in the system search path (like the SYSTEM32 directory), then only the DLL name is needed.

The Alias statement is used to define the exact name the compiler defines for this function. The __stdcall calling conventions dictate that the function name is prepended by an underscore ("_"), and appended by an at-sign ("@") followed by the size in bytes of all the parameters. Since the example function is passing a 4-byte integer, then the name will have "@4" appended. Note that you can omit the Alias clause if you make an entry in the .DEF file for the custom DLL. If you add an entry for SampleFunction and SampleSubroutine, then the linker will create an alias without the underscore and the @XXX suffix.