Using C# classes in Xojo

Hi Jim,

I’ve got to your last bullet, but xojo says that MyXojoToDotNetClassLibrary does not exist, That’s the namespace name I should use, isn’t it?

Cheers,
Simon

Hi Simon,

You are looking for the actuall dll name, not the namespace. In my example, it was myclasslibrary.dll. The c# namespace doesn’t really matter.

Hi Jim,

Yes, sorry, I just got that. Now it says MyClassLibrary.InterfaceImplementation does not exist!

Also, where should I see the module and classes it should create? I see the dll name in contents on the left, but there’s no code, or at least it says “no editor”.

Simon,

If you completely expand the module on the left, you should see the classes generated by Xojo.

Hm, there’s nothing to expand!

There just a world icon, with my dll name next to it (TestyVS), but no expand button, and nothing underneath it. Double-clicking it does nothing, single-click shows a big “No Editor”.

Only this I changed about your code in VS is that I couldn’t reference System.Windows.Forms, for some reason, so I took that using statement out, and took out the MessageBox, for now. I add it as a reference, but it doesn’t appear in the references afterwards - wierd. Could that be preventing Xojo from seeing the InterfaceImplementation class?

Something is wrong!

*Only thing

Sorry, wasn’t thinking straight, forget what I said about Forms.

Now my code is exactly the same as yours, but still my dll name / world icon doesn’t expand.

I appreciate any advice you have!

Hi Simon,

Not sure what the issue is. I know it works for me and a few others here on the forum who have tried it. Did you register the dll with regasm? Also, if you can post a link to your project and c# solution! I’ll take a look at it.

Thanks,

Jim

Hi Jim, thanks - I wasn’t using regasm, as I ticked “register for Interop”, but I just tried adding that step - still no joy.

Here’s a link to the project and c# solution!

https://www.dropbox.com/sh/aq4nb110thuje6a/tEvjNQNSW3

Also a screenshot in that folder, showing Xojo with the dll reference that doesn’t expand.

Thank you!

Simon

Simon,

Since your VS solution used 2012, I went ahead and installed it. In any case, the problem was that you needed to change the properties for your application to make the c# assembly com visible. In VS Right click on TestyVS go to properties->application->Assembly Information and click “Make Assembly COM-Visible”. Rebuild your solution in Visual Studio and in Xojo insert the class again. Run your Xojo application and enjoy.

P.S. Sorry if I didn’t make that step clear in my example.

Have Fun!

Jim

Aha, it works! So there are 4 things to check in the VS project:

1 - run VS as admininstrator.
2 - make COM-visible
3 - register for COM interop
4 - platform target is x86

Maybe also use regasm - although you implied that doing check 3 above would make that not necessary. I haven’t tried though.

I have also tested it as a Xojo web app, not desktop app - it also works. That’s fantastic!

Thank you so much for helping, and the effort to install VS2012. I’m looking forward to seeing what Xojo can do.

Best wishes,
Simon

Hi Simon, glad that worked.

You can do it either way. This really only becomes important if you need to change the physical location of the DLL on your hard drive (or end users hard drive). Most good installers will handle this registration for you as well.

Thanks,

Jim

I’ve been able to get this working using the Unmanaged Exports – which is HUGE for folks who need to create DLL’s and want to take advantage of .NET and the whole C# ecosystem.

This avoids the whole COM-Visible and COM-interop nightmare which can slow down a project, and also complicate things.

“Unmanaged Exports” a C# class library package by Robert Giesecke.

No more ActiveX wrapper! Excellent stuff!

Here’s what you do:

  1. Get the NuGet packages for VS 2010 or better, install them in your VS 2012 system.
  2. Create a VS C# Class Library Project
  3. When you create a method make sure its “static”, and that you decorate it with [DllExport]
  4. Compile the project, place the DLL in the runtime location of your Xojo project or make sure the Declare has a direct path
    to that DLL
  5. Voila! .NET DLL access from Xojo… no ActiveX wrapper, no COM-Interop.

Below is a code snippet.

C#

using System;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

public class TestExport
{
 
    [DllExport]
    public static int Test(string TestString)
    {
        return TestString;
    }
}
Declare Function Test lib "TestExport.dll" (msg as CString) AS String
 
 Msg ( Test ("Hello, .NET Class Library DLL!"))

Hi Gary, could your solution be used for non-static classes?

It’s not really “my” solution. Its the solution proposed earlier using the Unmanaged Exports class.

And no, I believe your methods need to be static.

Thanks, it’s great that it works.

Hi all,

This thread is fantastic! We’ve managed to get this working brilliantly. I do have a question about .dll’s though… Our .Net compiled dll calls a number of other dll’s. We’re finding that we need to manually add them to our debug build folder next to the application in order for the debug build to work correctly (i.e. before we call the function that access the ell).

Is there a way to add the dll’s to the build folder automatically at runtime or can I put them somewhere else so that my debug builds function properly? I’d like to say I’m an expert coder and don’t ever need to test a debug build…but I’m far from it :wink:

Cheers,
James

Hi James,

I do this using the copy files build step feature of Xojo.

  • Jim

Hi Jim,

I’m unfamiliar with this. Can you point me to where I should be looking?

Thanks again,
James