Calling native functions in dynamic link libraries

Epson RC+ 8.0 allows you to call native functions in Dynamic Link Libraries (DLLs).

This is used for complicated arithmetic processing and call for a native function of an external device.

To call the native DLL function, use a Declare statement which is a function definition command from the SPEL+ program and write a function call as normal.

For details, refer to the following manual.

"SPEL+ Language Reference - Declare"

Sample of calling a native DLL

By using a development tool such as Microsoft Visual Studio 2019, you can create a native DLL that can be called from SPEL+. Here, it uses Visual Studio 2019 as a sample to create a function that executes the arithmetic operator.

Step 1: Decide on variable type for a native DLL

You need to plan the data type to use for transferring with the native DLL in the Epson RC+ 8.0. Correspondence table for the Epson RC+ 8.0 data type and the C/C++ variable type is shown below. You cannot use the C/C++ byte type and structure because the Epson RC+ 8.0 has no correspond data for them.

Data correspondence

Epson RC+ 8.0 C/C++
Boolean short
Byte short
Short short
Integer short
Long int
Real float
Double double
String char [256] * Null included

Step 2: Create a native DLL

  1. Start Visual Studio 2019.

    Select the "Create a new project" on the start window.

  2. [Create a new project] dialog box will appear.

    1. Select “Windows Desktop Wizard” from the project template list shown on the right of dialog box.

    2. Click the [Next] button.

  3. Start the Windows Desktop Wizard.

    1. Type in a name for a project in the [Project name] box. (Here types in "MyCalculator".)

    2. Click the [Create] button.

  4. Set the project options.

    1. Select the “Dynamic Link Library (.dll)” in the [Application type].

    2. Check the [Export symbols] box in the [Additional options:].

    3. Click the [OK] button.

  5. A simple example of function “fnMyCalculator” will be created in MyCalculator.cpp. Add a function MyArithmetic which executes the arithmetic operator to this file.

    MYCALCULATOR_API float MyArithmetic(short value1, short value2, char * kind )
    {
      if ( !strcmp(kind, "add") )
      {
        return (float)(value1 + value2);
      }
      else if ( !strcmp(kind, "sub") )
      {
        return (float)(value1 - value2);
      }
      else if ( !strcmp(kind, "mul") )
      {
        return (float)(value1 * value2);
      }
      else if ( !strcmp(kind, "div") )
      {
        return (float)(value1) / (float)(value2);
      }
      else
      {
        strcat_s(kind, 10, " NG");
        return 0;
      }
    }
    
  6. Export a function to enable it to be called from SPEL+.

    1. Select Visual Studio 2019 menu-[Project]-[Add New Item]. The [Add New Item] dialog box will appear.

    2. Select the [Visual C ++]-[Code] in the tree on the left.

    3. Select “Module-Definition File (.def)” from the project template list shown on the center of dialog box.

    4. Type in a file name in the [Name:]. (Here types in "MyCalculator.def" as a file name.)

    5. Click the [Add (A)] button.

    6. Register “fnMyCalculator function” and “MyArithmetic function” to the created “MyCalculator.def” file.

      LIBRARY "MyCalculator"
      EXPORTS
      fnMyCalculator
      MyArithmetic
      
  7. Build the project and create the DLL. Select [Win32] as a solution platform for Visual Studio 2019. Then, select Visual Studio 2019 menu-[Build]-[Build MyCalculator]. DLL will be successfully created if no error occurs.

KEY POINTS


In Epson RC+ 8.0, 64 bit native DLL is not available. When using a DLL created with a version older than Visual Studio 2015, it is necessary to install the runtime corresponding to that version in advance.

Step 3: Call the DLL function from SPEL+

KEY POINTS


You can now try your DLL function from SPEL+. Before you call your function from the Epson RC+ 8.0, you must debug it and check thoroughly if it can work without errors. In case that error occurs (such as system error) in the native function, the Epson RC+ 8.0 will not work normally.

  1. Copy the created MyCalculator.dll to the Epson RC+ 8.0 project folder (e.g. C:\EpsonRC80\projects\dllcall).

  2. Define a DLL function which executes the arithmetic operator in the SPEL+ program and write a function call for MyArithmetic in Function main.

    Declare MyArithmetic, "MyCalculator.dll"(value1 As Integer, value2 As Integer, ByRef calc$ As String) As Real
    Function main
      Real result;
      String calc$
    
      calc$ = "add"
      result = MyArithmetic(1, 2, ByRef calc$);
      Print "1+2=", Str$(result)
      calc$ = "sub"
      result = MyArithmetic(1, 2, ByRef calc$);
      Print "1-2=", Str$(result)
      calc$ = "mul"
      result = MyArithmetic(1, 2, ByRef calc$);
      Print "1*2=", Str$(result)
      calc$ = "div"
      result = MyArithmetic(1, 2, ByRef calc$);
      Print "1/2=", Str$(result)
    Fend
    
  3. Build and execute the project. The following result will be displayed.

    1+2=3
    1-2=-1
    1*2=2
    1/2=0.5
    

KEY POINTS


Before you build the project, be sure to copy the native DLL to the project folder without fail. If you fail, a warning or error will occur.

Please note the dependency of the DLL when using a third party DLL as a native DLL. If the dependent DLL does not exist in the project folder or the folder set in the environment variable PATH of Windows, a warning or error will occur.