Calling X-Plat C# library in Xojo

Anyone done much work with UnmanagedExports?
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports#TOC-Marshalling-sample

It works for windows, not sure if it can be compiled .NET Standard and used with MacOS. I’m not even sure how to get Xojo to “include” a .dll library…

including a dll is pretty simple regardless of the language it comes from (or platform)

I created a sample in Rust but the Xojo portions should apply equally well to C#
https://www.great-white-software.com/blog/2020/04/14/creating-a-rust-dylib-we-can-call-from-xojo/

Its just a matter of having a postbuild step that copies your dll into the Frameworks dir of a built app. Then you use declares to access that dll

The bigger trick is getting the C# code to NOT mangle names etc and to use types that are compatible with Xojo’s
COM interfaces can be tricky to use from Xojo
They wont all work

Thanks, Norm. I was thinking that a .dll would only work on Windows and MacOS/Liinux needed .dylibs or something very different.

I thought with this unmanageddll library, COM was not something that was used?

Windows, no problem. MacOS, can’t seem to get things working.
On MacOS, I have the build step to copy the dll to the framework folder, but it’s not working.
If I leave “soft” unchecked on the external definition, I get this error:

Linking Executable ld: framework not found ClassLibrary1.dll

If I check “soft”, it runs, but when I call the function I get “FunctionNotFoundExeception”

Again, under Windows, no problem.

well to call it on macOS it would have to BE a dylib on macOS
but thats just the specific format for the platforms “dynamic library” - on Windows its a DLL on macOS its a dylib on Linux its a .so
I have no experience with doing with with C# so take this with a grain of salt

I just looked at the link you’d posted and saw there was COM in that
If you’re not using COM then it shouldnt be a problem

Ok, no problem.

Hoping that perhaps someone here has tried this UnmanagedDLL function and can comment.

Anyone?

@eugenedakin perhaps?

Hi Andy,

I am back from the field and am back on my development computer. When looking through the example, it appears that the examples are using COM.

LOL, its been a long day today - could you help me understand again what it is you are wanting?

Feel free to correct me, and I believe this is a request for an example that creates a C# dll library that can be loaded into Xojo, and the dll library functions can be called in Xojo. Am I correct?

Thanks a bunch :slight_smile:

I just tried to create a wide variety of dll’s with C# and wasn’t able to make it work. Sorry.

The good news is the C++ dll works well:
Create a Raw C++ dll for Xojo on my website.

Thanks so much, @eugenedakin!

Are you using the “dllExport” package?
Were you unable to make the Windows example work even? That seems to work fine for me.

I’m trying to see if dllExport package will work under MacOS. The author doesn’t check e-mails it seems, and it’s not specified on his site.

Just out of curiousity, does your C++ dll work on MacOS?

I tried, and couldn’t seem to find instructions that ran. I always ended up with errors and the dll could not be created. This was all on Windows 10.

I haven’t tried it on a Mac, as mine is VERY old :slight_smile: Its a late 2009 3.1 model 220px-Mac_Mini_(early_2009)_7786

What OS version?

Ok, that’s strange! What errors specifically?

I didn’t have much trouble, using the examples here
or here.

I’m away for the weekend, but next week I’ll link my Xojo and C# project.
I was using Xojo 2019r1.1 and VS2019.

Lol, Snow Leopard.

This might interest you.
I have Catalina running on my old 2009 MBP…

Its all good, there is no rush. Here are the errors that I have along with the instructions on trying to get it running:

  1. Start Visual Studio 2019 in administrator mode
  2. Create a new project button
  3. Create a ‘Class Library (.NET Framework) ->Next
  4. Save file as ‘XojoCL’
  5. Go to the toolbar: Tools->NuGet package manager->Package Manager Console
    a. A Package manager console tab is opened at the bottom of the Visual Studio IDE with ‘PM>’
    PM> ‘Install-Package UnmanagedExports -Version 1.2.7
  6. Press enter to install this package.
  7. Select Build->Configuration Manager
    a. Active solution configuration->Release
    b. Active solution platform->New->x64
  8. Tools->NuGet Project Manager->Manage NuGet Packages for Solution
  9. Search for DllExport
  10. install
  11. Select x64 , check for proper solution (sln) path, press apply
  12. Select XojoCL project
  13. Restart Visual Studio and reload the XojoCL project.
  14. Add the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

public class TestExport
{
[DllExport]
public static int AddTwo(int a, int b)
{
return a + b;
}
}

Here are the errors:
1>------ Rebuild All started: Project: XojoCL, Configuration: Release x64 ------
1> XojoCL -> C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\XojoCL\bin\x64\Release\XojoCL.dll
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: The “DllExportAppDomainIsolatedTask” task failed unexpectedly.
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: System.ArgumentException: Requested value ‘Version47’ was not found.
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018:
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: Server stack trace:
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.ExportTaskImplementation1.<>c__DisplayClass8.<GetGetToolPathInternal>b__7(Version version, String toolName) in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\ExportTaskImplementation.cs:line 568 1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.ExportTaskImplementation1.TryToGetToolDirForFxVersion(String toolFileName, Func3 getToolPath, String& toolDirectory) in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\ExportTaskImplementation.cs:line 725 1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.ExportTaskImplementation1.ValidateToolPath(String toolFileName, String currentValue, Func3 getToolPath, String& foundPath) in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\ExportTaskImplementation.cs:line 698 1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.ExportTaskImplementation1.ValidateFrameworkPath() in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\ExportTaskImplementation.cs:line 680
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.ExportTaskImplementation1.ValidateInputValues() in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\ExportTaskImplementation.cs:line 396 1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.ExportTaskImplementation1.Execute() in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\ExportTaskImplementation.cs:line 264
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at RGiesecke.DllExport.MSBuild.DllExportAppDomainIsolatedTask.Execute() in c:\Users\rober_000\Documents\Code\unmanaged-exports\RGiesecke.DllExport.MSBuild\DllExportAppDomainIsolatedTask.cs:line 241
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object args, Object server, Object& outArgs)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018:
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: Exception rethrown at [0]:
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at Microsoft.Build.Framework.ITask.Execute()
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
1>C:\Users\eugen\Desktop\New\NetFromXojo\XojoCL\packages\UnmanagedExports.1.2.7\tools\RGiesecke.DllExport.targets(58,3): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

1 Like

Of course, after all the hoops I tried jumping through to get to work x-plat, I no longer can get the Windows one to work anymore.
BUT, it looks like this functionality will be coming to a .NET near you…


Scroll down about half way to the section called “Native Exports”. Seems like they have some work to do, but this certainly looks promising, especially since they specifically state it’ll be x-plat.

I’m going to play with the code they linked to and see if I can get something working.
It’s unfortunate that Xojo gives no more details to what the exact problem is other than “function not found” if the format of the dll is not to it’s liking…

Thanks for your help with this, @eugenedakin and @npalardy!

1 Like

Nice list of features !

These two look fun !

Support Single-File Apps in .NET 5
.NET 5.0 also includes support for Web Assembly

At the end of 2021 there’s a lot of good promises with NET6. THAT will light up my interest in .Net again.