vendredi 4 septembre 2015

Switch Case with parameter passed from VBA - parameter not compatible

this is an odd behaviour which I encouter. I have a Visual Basic Interface for the User.

Declare PtrSafe Sub getPEC _
Lib "C:\Users\...somePath...\OPunit0011PUMP.dll" _
(ByVal typeOfPump As Integer, _
ByVal outflow As Double, _
ByRef pec As Double, _
ByRef RV As Integer)

The user specifies a pump by the integer typeOfPump. I pass this parameter as ByVal typeOfPump into my C++ DLL. Here according to which pump it is some predeclared parameters a,...f are initialized using switch case

extern "C"

{

double PEC_backUp;

void __stdcall getPEC(int typeOfPump, double outflow, double &PEC, int &RV)
{
    //polynomal trendline for different pumps
    //y = a x^6 + b x^5 + c x^4 + d x^3 + e x^2 + f x^1 + g x^0
    if (typeOfPump < 1)
    {
        RV = -1;
        return;
    }

    double a, b, c, d, e, f, g;
#pragma region switch case
    switch (typeOfPump)
    {

        //150 bar
    case 1:
        a = 1.1186E-08;
        b = -1.49172E-05;
        //...
        break;
   case 2:
        //...
   }

My problem is that switch case does NOT work. My default value is set to nine, but also every other case does NOT work. It simply neglects the switch case code. Note also: The same odd behaviour can be seen in the If condition:

if (typeOfPump > 1)
    {
        RV = -1;
        return;
    }

Despite the fact that typeOfPump is assigned to NINE which is obviously bigger than one my function getPEC does not return at this point. On the other hand if I write

if (typeOfPump < 1)
        {
            RV = -1;
            return;
        }

my function will return here. I then assigned the value of typeOfPump to RV to monitor it in VBA and RV was set to nine.

Moreover, to make things even stranger it automatically changes the value of pec to 7.00000000005821 (using watch function of VBA) when it returns with RV = -1.

I guess my parameter are somehow not compatible for operations in my DLL. Did anyone see this before and how can I fix it? Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire