This example shows how to create a simple function that adds two numbers together by creating a simple DLL in C++ with Visual Studio 2019 that can be used in Xojo 2019 r1.1. The secret is that the functions will need to have "C"-like names to prevent name mangling. This example is for 64-bit applications.
Here are the steps to create the plugin in Visual Studio 2019 and read the DLL in Xojo.
- Start Visual Studio 2019
- Create a new project
- Select Dynamic-Link Library with exports (DLL)
- Create project name RawPluginCPlusPlus code
Add code to RawPluginCPlusPlusCode.cpp
// RawPluginCPlusPlusCode.cpp : Defines the exported functions for the DLL. // #include "pch.h" #include "framework.h" #include "RawPluginCPlusPlusCode.h" // This is an example of an exported function. RAWPLUGINCPLUSPLUSCODE_API int AddTwo(int x, int y) { return x + y; } |
Add code to RawPluginCPlusPlusCode.h
// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the RAWPLUGINCPLUSPLUSCODE_EXPORTS // symbol defined on the command line. This symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // RAWPLUGINCPLUSPLUSCODE_API functions as being imported from a DLL, whereas this DLL sees symbols // defined with this macro as being exported. #ifdef RAWPLUGINCPLUSPLUSCODE_EXPORTS #define RAWPLUGINCPLUSPLUSCODE_API __declspec(dllexport) #else #define RAWPLUGINCPLUSPLUSCODE_API __declspec(dllimport) #endif extern "C" RAWPLUGINCPLUSPLUSCODE_API int AddTwo(int x, int y); |
- Select ‘Release’ and ‘x64’ configuration in Visual Studio
- Select Build menu and press Build Solution
- Start Xojo 2019 r1.1
- Create a new Desktop project in Windows and save it as RawPlugin.xojo_binary_file
- Change the Xojo project to an x64 application
- Copy the dll file from C:\Users\Owner\Desktop\New\RawPlugin\RawPluginCPlusPlusCode\x64\Release\ RAWPLUGINCPLUSPLUSCODE.dll and paste it beside the RawPlugin.xojo_binary_file
- Add three TextFields to hold two numbers from the user (numbers to be added) and the last Textfield to show the answer.
- Add the following code to an action event
Sub Action() Handles Action Declare Function AddTwo Lib "C:\Users\Owner\Desktop\New\RawPlugin\RAWPLUGINCPLUSPLUSCODE.dll" (x as Integer, y as Integer) As Integer Dim Answer as Integer
If System.IsFunctionAvailable("AddTwo", "C:\Users\Owner\Desktop\New\RawPlugin\RAWPLUGINCPLUSPLUSCODE.dll") Then Answer = AddTwo(5, 3) //Add 5 + 3 Answer = AddTwo(TFFirstNumber.Text.CLong, TFSecondNumber.Text.CLong) Else Answer = 0 //Error End If
TFAnswer.Text = Answer.ToText End Sub |
The code can be downloaded at Github Raw Plugin.
Thats it! This program was created in Visual Studio C++ 2019, and Xojo 2019 r1.1 on the 23rd of September 2019.