18.3 Windows/Borland C 5.02

Making and using a DLL consists of three major steps

  1. Writing the C/C++ code
  2. Generating the DLL
  3. Using DLL in XploRe


Let us consider a simple C program vecsum.c which sums up a vector:

#include "dll.h"

EXTERN int EXPORT sum (double *x, double *n, double *s)
{
  long i, nn = (long) *n;
  *s=0.0;
  for (i=0; i<nn; i++)
    *s = *s + *(x+i);

  return (0);
}


Note that the include file dll.h defines the symbols EXTERN and EXPORT in a proper way for the below mentioned compiler.


As a first step we create a project called vecsum.ide to generate a Windows DLL:

  1. choose from the menu File/New/Project
  2. change ProjectPath to C:\vecsum\vecsum.ide
  3. choose under Target Type the second item Dynamic Library [.dll]
  4. change in the second box under Libraries: the radio button from Dynamic to Static
  5. decheck in the box Frameworks: the check boxes OWL and then Class Library
  6. press OK
  7. you may delete in the Project Window the files vecsum.rc and vecsum.def


The second step consists of generating the code and compiling it:

  1. in the Project window you get the context menu of vecsum.cpp with right mouse button and choose View/Text Edit
  2. in the editor window type now (or copy) the example code given above
  3. make sure that dll.h is in C:\vecsum
  4. save the file via the menu File/Save
  5. compile the DLL via the menu Project/Build all


To use the DLL in XploRe requires the following simple steps.

  1. Define the vector x and initialize the result s

    x=1:10 s=0
    

  2. Open the DLL for use in XploRe

    h= 41501 dlopen ("vecsum.dll")


    Note that XploRe needs to be able to find this library. From XploRe you can set the path for DLLs by

    41504 setenv ("xpl4dll","/your_absolute_path_to_the_dll")


    Alternatively you can give the absolute path within the dlopen command.

  3. Call the function sum from the DLL for x

    41507 dlcall ("_sum",x, rows(x),s)


    As a result you should find s having the value 55 which is the sum for the numbers 1 to 10.