Make and Call AddTwo from DLL

  1. 2 months ago

    Eugene D

    Aug 12 Pre-Release Testers, Xojo Pro Canada scispec.ca

    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:

    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

    This is the C++ MyMathDLL.h file:

    #ifdef MYMATHDLL_EXPORTS
    #define MYMATHDLL_API __declspec(dllexport)
    #else
    #define MYMATHDLL_API __declspec(dllimport)
    #endif
    
    extern MYMATHDLL_API int AddTwo(int x, int y);

    Here is the MyMathDLL.cpp 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;
    }

    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

    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:

    #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);

    Thanks for all of your help!

    Eugene

  2. Norman P

    Aug 12 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    xojo integer doesnt map to a C++ int

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

  3. Eugene D

    Aug 12 Pre-Release Testers, Xojo Pro Canada scispec.ca

    @Norman P xojo integer doesnt map to a C++ int
    for the xojo code use an int32 and see if that helps

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

    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
  4. Jeff T

    Aug 12 Pre-Release Testers Midlands of England, Europe

    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..

  5. Eugene D

    Aug 12 Pre-Release Testers, Xojo Pro Canada scispec.ca

    @Jeff T And to avoid that the function needed to be forced into a WIN32 format using _stdcall

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

  6. Andrew L

    Aug 12 San Francisco, CA, USA

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

  7. Jeff T

    Aug 12 Pre-Release Testers Midlands of England, Europe

    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

  8. Eugene D

    Aug 13 Pre-Release Testers, Xojo Pro Answer Canada scispec.ca

    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:

    #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);

    Thanks for all of your help!

    Eugene

or Sign Up to reply!