Announcement

Collapse
No announcement yet.

VC++ Simple DLL Example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • VC++ Simple DLL Example

    VC++ Simple DLL Example

    I am working on adding Automatic Trendlines to eSignal charts and need to be able to call a VC++ Regular DLL from an EFS script. I am new to VC++ and to DLLs and need help with a very simple call to an example DLL.

    I found a great web page at http://msdn.microsoft.com/en-us/library/ms235636(v=VS.90).aspx that stepped me through making the VC++ DLL. Thanks to this page, I had no problem making the VC++ DLL using Visual Studio 2008.

    I then created a very simple EFS to call the DLL. The EFS returns an error telling me that the called function was not found in the DLL.

    I have included the error and all of test code below in hope that someone will see why the error is returned. For the test I am using a 32 bit computer and have tried to name the DLL both MathFuncsDll.dll and MathFuncsDllx32.dll. I tried using the Externs “C” command but got VC++ syntax errors. I don’t know how to add it to the sample code.

    If anyone can help I would really appreciate it. I have tried to make this a very thorough post so that it can also help others with a complete example.

    The simple EFS:
    var dll = new DLL("MathFuncsDll.dll");
    var InitialPass = true;

    function preMain() {
    dll.addFunction("Subtract", DLL.DOUBLE, DLL.CDECL, "Subtract", DLL.DOUBLE, DLL.DOUBLE);
    }

    function main() {
    if ( isLastBarOnChart() == true ) {
    if (InitialPass = true) {
    var doub = "";
    doub = dll.call("Subtract", 5.5, 1.25);
    InitialPass = false;
    }
    }
    return
    }

    The EFS error returned:
    C:/Users/Andy/Documents/Interactive Data/Formulas/My Formulas/PTest.efs, line 13: Error: Function 'call': Failed to call dynamic library function: cannot find function 'Subtract' in library 'C:\Users\Andy\Documents\Interactive Data\Formulas\My Formulas\MathFuncsDll.dll'. Please specify correct dynamic library function name

    The DLL code in VC++:

    // MathFuncsDll.cpp
    // compile with: /EHsc /LD

    #include "MathFuncsDll.h"

    #include <stdexcept>

    using namespace std;

    namespace MathFuncs
    {
    double MyMathFuncs::Add(double a, double b)
    {
    return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
    return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
    return a * b;
    }

    double MyMathFuncs:ivide(double a, double b)
    {
    if (b == 0)
    {
    throw new invalid_argument("b cannot be zero!");
    }

    return a / b;
    }
    }
    // MathFuncsDll.h

    namespace MathFuncs
    {
    class MyMathFuncs
    {
    public:
    // Returns a + b
    static __declspec(dllexport) double Add(double a, double b);

    // Returns a - b
    static __declspec(dllexport) double Subtract(double a, double b);

    // Returns a * b
    static __declspec(dllexport) double Multiply(double a, double b);

    // Returns a / b
    // Throws DivideByZeroException if b is 0
    static __declspec(dllexport) double Divide(double a, double b);
    };

    }

    MathFuncsDll.def file:

    LIBRARY MathFuncsDll
    DESCRIPTION "MathFuncsDesc"
    EXPORTS
    Add @1
    Subtract @2
    Multiply @3
    Divide @4
    Andy Skinner

  • #2
    Deleted post
    Last edited by SteveH; 09-07-2011, 05:59 AM.

    Comment

    Working...
    X