Accessing .NET code from Xojo (C# custom DLL)

I am trying to apply the example described in Paul Lefebvre blog post
http://www.xojo.com/blog/en/2014/01/accessing-net-code-from-xojo.php

I am using Visual Studio 2013, and encountering difficulties. I was able to start a new C## class as described, and rename the file to XojoTest.cs. Now I have trouble with VS no recognizing some of the code.

The default using created by VS are as follow :

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

I added as instructed (System is part of the default) :

using System.Windows.Forms; using System.Runtime.InteropServices;

VS complains that :

Error 1 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) c:\\users\\michel\\documents\\visual studio 2013\\Projects\\XojoTest\\XojoTest\\XojoTest.cs 6 22 XojoTest

Then I paste the code as described, and I get another error :

Error 2 The name 'MessageBox' does not exist in the current context c:\\users\\michel\\documents\\visual studio 2013\\Projects\\XojoTest\\XojoTest\\XojoTest.cs 25 13 XojoTest

To go on with this and see how it goes on the Xojo side, I simply commented out the offending lines, and followed the instructions to make the build for x86 and Register for Interop. After which I built the DLL fine.

In the new Xojo project I was able to locate and add the Xojo Test reference fine. I placed the example code in a button and tried to run, then I got

There is no class by this name Dim o As New XojoTest.InterfaceImplementation

Which I believe is the reason why there is no item by that name in the next line.

As you see, I am completely stuck. Ultimately, my goal is to be able to build a DLL that returns a string, but if this very simple example does not work, I am very far to get anything done.

I will very much appreciate any help.

Thank you in advance.

Did you check the references for the project? If there is no System.Windows.Forms reference you will get that error when you try to compile the project.

Here are the references in the project, created automatically :

Microsoft.CSharp System System.Core System.Data System.Data.DataSetExtensions System.Xml System.XML.linq

I tried to right click on ‘References’ and select 'Add", but there is no way to add System.Windows.Forms.

I followed Paul Lefebvre instructions in the blog to the letter and I hope he will be able to assist. He mentions his example is with VS 2012 but thinks it is good for 2013. My issue is that I do not have VS 2012 and do not really feel messing up my system with some shady torrent download, since 2012 is no longer available on the Microsoft site.

Michel,

VS2013 works fine. Paul’s blog was based on this forum posting where some of theses issues were discussed and resolved.

here

[quote=130162:@Jim Cramer]Michel,

VS2013 works fine. Paul’s blog was based on this forum posting where some of theses issues were discussed and resolved.

https://forum.xojo.com/7079-using-c-classes-in-xojo[/quote]

Thank you Jim. Although I do not find the issue with System.Windows.Forms mentioned there. And why does MessageBox does not exist in the current context.

Well messagebox is part of the system.windows.forms namespace so I would imagine that if you resolve your reference to system.window.forms you’ll resolve that issue as well.

What version of vs2013 are you using? As far as I know, you need the “desktop” version (not the “windows” version) to be able to add that reference.

All credit to Jim Cramer. He’s the one that figure it out the technique.

You’re getting the MessageBox error because is exists in the System.Windows.Forms namespace, which you’ve not been able to load. I’m not sure of the reason. I’d expect you’d simply be able to add the reference to the project.

[quote=130180:@Jim Cramer]Well messagebox is part of the system.windows.forms namespace so I would imagine that if you resolve your reference to system.window.forms you’ll resolve that issue as well.

What version of vs2013 are you using? As far as I know, you need the “desktop” version (not the “windows” version) to be able to add that reference.[/quote]
Microsoft Visual Studio Express 2013 for Windows Desktop
Version 12.0.21005.1 REL

I just found the assembly reference I need for system.Windows.Forms :slight_smile:

Excellent!

OK. Now I was able to build this without error and got a nice DLL :

[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace XojoStuff
{
[Guid(“DB038D2E-BD4D-44A7-BE64-4844FF07F870”)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IManagedInterface
{
[DispId(1)]
int PrintHi(string name);
}
[Guid(“AB46D07F-6920-4114-924B-5E37BED3A65E”)]
[ClassInterface(ClassInterfaceType.None)]
public class InterfaceImplementation : IManagedInterface
{
public int PrintHi(string name)
{
MessageBox.Show(name);
return 1;
}
}
}
[/code]

Now in the Xojo project, I was able to add the DLL and it shows as XojoTest, but yet, I get

There is no class by that name Dim o As New XojoTest.InterfaceImplementation

Michel,

Did you follow the procedure in VS2013 to make your DLL com visible (properties->application->Assembly Information and tick “Make Assembly COM-Visible”)?

Did you start with a new Xojo project or delete and re add the XojoTest module? Since the com interop module is generated by the Xojo IDE, it needs to be deleted and re-added if anything changes with the public interface for your external DLL.

That was it :slight_smile:

Thank you Jim. It works as described :slight_smile:

Here’s a link to some example VB.Net code:

http://www.codeproject.com/Articles/3511/Exposing-NET-Components-to-COM

[quote=130214:@Johnny Harris]Here’s a link to some example VB.Net code:

http://www.codeproject.com/Articles/3511/Exposing-NET-Components-to-COM[/quote]

Great. Thank you :slight_smile:

[quote=130214:@Johnny Harris]Here’s a link to some example VB.Net code:

http://www.codeproject.com/Articles/3511/Exposing-NET-Components-to-COM[/quote]

Thank you for that link. I have been able to apply the examples in there fine, in particular the one from VB.NET which can be precious to bring to Xojo typical .NET features otherwise not available in Win32 without having to cope with a totally alien language such as C##.

The difficulty I face now is : in both Jim/Paul example and this one, the variables returned by the DLL are numeric. And it works fine. But what about exchanging strings, both as arguments and as return value from the DLL ?

Has anybody done that already ? How ?

To be honest Michel I’ve not messed with this much. I found the Code Project link trying to figure out how to do the same in VB. I took the VB example and created a couple of functions that did some date calculations by passing dates as strings to the .dll functions, but they all returned integers. If I get some time today, I plan to look at it more in depth.

I tried to tweak the VB .NET example code to have it return a string, but the DLL became invisible in Xojo.

The workaround I found was to create a Console program instead, so I can pass arguments with the shell call and get the string back in result. It works quite fine, but a DLL would probably be faster.

I will continue my investigations and will post the solutions I find if any.

Thank you.

Michel I don’t seem to be having any problems passing strings or integers back and forth between Xojo and the .dll.

I have noticed that I have to close Xojo or VS won’t compile the .dll.

[quote=130486:@Johnny Harris]Michel I don’t seem to be having any problems passing strings or integers back and forth between Xojo and the .dll.

I have noticed that I have to close Xojo or VS won’t compile the .dll.[/quote]

I will try again with a string, then.

Yes, when you add the DLL as control x, it makes the file busy, so VS cannot build it. Closing the Xojo project is is necessary to be able to build the DLL again.

The DLL is probably loaded & marked busy which prevents it being replaced