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 2020 r1. The two major aspects are to: 1) Install the Microsoft Build Tools 2015, and 2) RGiesecke NuGet package.
*Uses Xojo API2
Here are the steps to create the plugin in Visual Studio 2019 and read the DLL in Xojo:
- Download Microsoft Build Tools 2015 and install from: https://www.microsoft.com/en-us/download/confirmation.aspx?id=48159
- Start Visual Studio 2019
- Create a New Project
- Select C# Class Library (.NET Framework) and press Next
- Create a folder where to store project (I am using C:\test\CSharpXojo\)
- Project name is CSXojo and press Create
- Tools->NuGet package manager->Manage NuGet Packages for Solution
- Browse and choose->Unmanaged Exports by Robert Giesecke->Click on CSXojo in right panel->Select Install
- Select Release, CPU Manager and choose Configuration: Release, Platform: x64, Build (checked)
- The following code in Class1.cs should look like the following:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RGiesecke.DllExport; namespace CSXojo { public class Class1 { [DllExport] public static int AddTwo(int x, int y) { return x + y; } } } |
- Select Build->Build solution
- Create a new Xojo desktop project
- Save the project in C:\test and name the project XojoCSharp.xojo_binary_project
- Select Windows x86 64-bit build
- Add a label and a button, add action event to button and the following code:
Declare Function AddTwo Lib "C:\test\CSharpXojo\CSXojo\CSXojo\bin\x64\Release\CSXojo.dll" (x as Integer, y as Integer) As Integer LblAnswer.Text = CStr(AddTwo(145,1023)) |
Run the 64-bit Xojo program and pressing the OK button will show an answer of 1168. Well done!!!!!
The code is available on Github at: CSharpDLLXojo Github Link