Home > Software engineering >  C DLL Not Receiving Passed Variables
C DLL Not Receiving Passed Variables

Time:02-11

I am trying to write a C DLL that will receive two long values, sum them, and then return the result. I'm doing this testing as a way to attempt to fix a 20 year old C DLL that needs some fine-tuning to be 64-bit compliant and my skills in C are rusty at best. So I'm starting simple with basic math and I can't seem to get it to work. I'm calling the DLL from VBA but I don't think it's my VBA that's the issue.

C Header File

#pragma once

#define DLL_EXPORT _declspec (dllexport)

class __declspec(dllexport) CalculationApi
{
public:
    long Addition(long* value1, long* value2);
};

C CPP File

#include "pch.h"
#include "Calculation.h"
#include "windows.h"
#include "string"
#include<iostream>

using namespace std;

long DLL_EXPORT __stdcall CalculationApi::Addition(long* value1, long* value2)
{
    long return_result;
    string MsgBoxMessage;

    string Integer1_Type = typeid(*value1).name();

    string test_integer_text = std::to_string(*value1);

    MsgBoxMessage = "Value1 is: "   to_string(*value1);
    MessageBox(NULL, MsgBoxMessage.c_str(), "Value of Value1", MB_OK);

    MsgBoxMessage = "Value1 Data Type is: "   Integer1_Type;
    MessageBox(NULL, MsgBoxMessage.c_str(), "Data Type of Value1", MB_OK);

    MsgBoxMessage = "Value2 is: "   to_string(*value2);
    MessageBox(NULL, MsgBoxMessage.c_str(), "Value of Value2", MB_OK);

    return_result = *value1   *value2;
    
    *value1 = 50;

    MsgBoxMessage = "Value1 is: "   to_string(*value1);
    MessageBox(NULL, MsgBoxMessage.c_str(), "Value of Value1", MB_OK);

    
    MsgBoxMessage = "Result is: "   to_string(return_result);
    MessageBox(NULL, MsgBoxMessage.c_str(), "Math Results", MB_OK); 

    return return_result;

}

And just in case I messed up the VBA here's how I call the DLL

Private Declare PtrSafe Function Addition Lib "C:\Users\micha\source\repos\CalculationDll\x64\Debug\CalculationDll.dll" (value1 As LongPtr, value2 As LongPtr) As LongPtr   

Function DLL_Testing()
    Dim value1 As LongPtr
    Dim value2 As LongPtr
    Dim Results As LongPtr
        
    value1 = 10
    value2 = 20
    
    Results = Addition(integer1, integer2)
    
    test = 0

End Function

The first messagebox in the C comes up blank... I'm sure I'm just not doing something right there but meh doesn't matter much. The second messagebox returns either 0 or some really funky number. The VBA will return the result "583141696^" which of course is not 10 20. Also the value1 doesn't get updated to 50, I'm pretty sure I'm not passing the pointer correctly as I've seen value2 update when I changed the value of value1 in the DLL.

This all seems REALLY basic yet it doesn't work. Thoughts?

CodePudding user response:

I think Addition should be static :

static long Addition(long* value1, long* value2);

because if Addition is not static, a third (hidden) parameter (this) exist...

and the class CalculationApi is not really usefull here.

CodePudding user response:

Well the suggestion to convert this to C instead of C paid off (after finding a dumb error of my own). Changes I made...

  • Class gone
  • Converted to C

I couldn't get the "static" suggestion to work but the code in C works fine without it. Not sure what's up there but I'm going to go with it.

  • Related