(Solved) – Wrong outputs from dll c++ to vba

(solved)-–-wrong-outputs-from-dll-c++-to-vba

I created a DLL in C and I call it through Excel VBA. The DLL includes a function with 3 arguments. When I run the function in Excel, I observe the following:

  • In the first if statement, the function returns zero
  • In the second and third statements, the function returns the right value

I cannot understand why it happens. Could you please help me?

C code

#include 
#include  
#include  

using namespace std;

double _stdcall
Tables(int &a, int &b, double &c)
{
    double q[125];
    if (b == 1)
    {
        ifstream inFile("path");    
        for (int Pntr = 1; Pntr < 126; Pntr  )
        {
            inFile >> q[Pntr - 1];
        }
        return q[a] * c;
    }
    else if (b == 2)
    {
        ifstream inFile("path");
        for (int Pntr = 1; Pntr < 126; Pntr  )
        {
            inFile >> q[Pntr - 1];
        }
        return q[a];
    }else if (b == 3)
    {
        ifstream inFile("path");
        for (int Pntr = 1; Pntr < 126; Pntr  )
        {
            inFile >> q[Pntr - 1];
        }
        return q[a];
    }
    return 1000;
}

VBA code

Public Declare PtrSafe Function propTable Lib "dll path" (a As Long, b As Long, c As Double) As Double

Public Function EAE1990m(a As Long, b As Long, c As Double) As Double
    EAE1990m = propTable(a, b, c)
End Function

Leave a Reply

Your email address will not be published. Required fields are marked *