Access C# DLL

I need to access a third party, .Net DLL that was written in C#. I found an old blog post from January 3, 2014 that shows how to convert it to a COM object so it can be accessed with Xojo. Unfortunately, I don’t know enough about C to convert the code for my use. This is the code from the blog.

using System;
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; } }
}

I used Regedit to find the dll which gives me the second GUID. I have no idea where I would find the first GUID.

I need to be able to send a string to the DLL that contains a command plus zero or more parameters and get back string with the value returned by the DLL which I believe requires modification to the “public interface” and “public class” lines above.

I am hoping that I can use something like

Var o As New XojoText.InterfaceImplemention
call o.DoFunction(“GetKWH(phase=1)”)

rather than having to implement each function of the DLL I might want to use such as

call o.GetKWH(“phase=1”)

or

call o.SetVoltage(“phase=1,volts=120”)

Any help with this would be greatly appreciated.

If it’s a COM object you might be able to bring it into Xojo by inserting it as an ActiveX component. I’ve had mixed results with this in Xojo, sometimes the code that is generated has the wrong datatypes so you might need to do some tinkering. Have you tried this approach?

Right now I am still trying to make a COM object out of a managed .NET object.

if you only need a single functionality a c# commandline tool project is maybe a option, and call it from xojo.

1 Like

I seem to remember that you can write a wrapper class in C or C++ to access C# libraries using declares.

If its your C# object or object you have the source code for then you can put attributes in it so that it gets COM wrapper built into it.

If its not object you own then you can make your own C# object that wraps the other one where your object would then register it self as COM object with the correct attributes.

How good or nice or bad its to talk to COM object in Xojo I dont know though.

1 Like

If you have the C# source code, you can build the dll and build it into a plugin. Here is example code GitHub - eugenedakin/CSharpDLLXojo: Build a DLL in CSharp with External Functions for Xojo with a link to the article at Create a CSharp DLL for Xojo for further help.

2 Likes

what is the dll name and usecase?
if this is open source it could already exists/converted in other languages.