Make and Call AddTwo from DLL

Hello Everyone,

I am trying to build a C++ dll with Visual Studio and have the function work in Xojo. The example is created in 64-bit Windows 10, and I can’t seem to get Xojo to recognize that the function is available and to run the example dll. For testing purposes, it is a method called AddTwo, which adds two integers together and returns the sum of these two numbers.

I must be missing something simple. Included is the Visual Studio Code, DLL, and Xojo Code.

Here is the Action Button code:

[code]Declare Function AddTwo Lib “MYMATHDLL.dll” (x as Integer, y as Integer) As Integer
Dim Answer as Integer

If System.IsFunctionAvailable(“AddTwo”, “MYMATHDLL.dll”) Then
Answer = AddTwo(5, 3) //Add 5 + 3
Else
Answer = 0 //Error
End If

TextArea1.Text = TextArea1.Text + "answer: " + Answer.ToText[/code]

This is the C++ MyMathDLL.h file:

[code]#ifdef MYMATHDLL_EXPORTS
#define MYMATHDLL_API __declspec(dllexport)
#else
#define MYMATHDLL_API __declspec(dllimport)
#endif

extern MYMATHDLL_API int AddTwo(int x, int y);
[/code]

Here is the MyMathDLL.cpp code:

[code]// MyMathDLL.cpp : Defines the exported functions for the DLL.

#include “pch.h”
#include “framework.h”
#include “MyMathDLL.h”

// This is an example of an exported function.
MYMATHDLL_API int AddTwo(int x, int y)
{
return x + y;
}[/code]

After trying this for a couple of days, its time for me to ask for help.

Here is the Visual Studio 2019 C++ code and Xojo file: MyMathDLL.zip

Thanks for your help,

Eugene

xojo integer doesnt map to a C++ int

for the xojo code use an int32 and see if that helps

[quote=450303:@Norman Palardy]xojo integer doesnt map to a C++ int
for the xojo code use an int32 and see if that helps[/quote]

Thanks for the suggestion, and it returned the value of 0 in the IsFunctionAvailable section. :slight_smile:

[code]Sub Action() Handles Action
Declare Function AddTwo Lib “MYMATHDLL.dll” (x as Integer, y as Integer) As Integer
Dim Answer as Int32

If System.IsFunctionAvailable(“AddTwo”, “MYMATHDLL.dll”) Then
Answer = AddTwo(5, 3) //Add 5 + 3
Else
Answer = 0 //Error
End If

TextArea1.Text = TextArea1.Text + "answer: " + Answer.ToText
End Sub
[/code]

Its been a long while since I last built a DLL, but I seem to recall that a C++ function presents with a different name than a pure C function, because the name gets stuff added to the end that represents the parameters (?)
And to avoid that the function needed to be forced into a WIN32 format using _stdcall

Apologies if this is wildly wrong, my memories of this stem from VB5 days and using C and Delphi to create DLLs…

Thanks for the suggestion Jeff. Its bedtime now, and I’ll try your suggestion tomorrow after work.

See: https://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function

Good article Andrew.
Assuming it is this name mangling which is the issue, one or more of the options on that page will assist.
Right now, the function is probably called AddTwo@2 or similar in the DLL

Thanks Jeff and Andrew,

Andrew and Jeff were correct, it was the mangling of the C++ name that caused the issue. The updated working MyMathDLL.h code is shown below:

[code]#ifdef MYMATHDLL_EXPORTS
#define MYMATHDLL_API __declspec(dllexport)
#else
#define MYMATHDLL_API __declspec(dllimport)
#endif

extern “C” MYMATHDLL_API int AddTwo(int x, int y);[/code]

Thanks for all of your help!

Eugene