Using C# classes in Xojo

  1. 6 years ago

    Hi,

    I am evaluating Xojo for developing a commercial web app, and I understand it is possible to use some C# classes that I have already developed. These C# classes have no user interface - they are just inter-related classes that carry out calculations, and that read and write to and from a Mongo database.

    Please can somebody describe how I can use my C# classes in Xojo?

    I am trying to find out:

    • what kind of project should my C# classes be contained or wrapped in? E.g. is it a Class Library, with "Make assembly COM-Visible" ticked? Or something else?
    • what kind of classes do they need to be? E.g. I assume they need to be public classes - are they any other requirements?
    • what other properties do I need to check or set in my Visual Studio project, to make it usable within Xojo? E.g. do I need "Register for COM interop" ticked?
    • what do I need to do outside of Visual Studio or Xojo, to make it possible to see and use my C# classes from within Xojo? E.g. do I need to use regsvr32, or regasm, and with what parameters?
    • what do I need to do in Xojo to be able to instantiate and use my C# classes? E.g. should I use Insert > ActiveX Component, and look on the Components tab, or the References tab?

    I have tried out a few things, from searching the forums and the internet, but none of it seem to work, in the sense that I cannot see my C# library on either tab of the Insert > ActiveX Component command.

    I am using Xojo version 2013, release 3.3, and Microsoft Visual Studio 2012 Ultimate, update 4. I'm using 64-bit Windows 7 Professional, service pack 1.

    Thanks!
    Simon

  2. Norman P

    27 Nov 2013 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...
    Edited 6 years ago

    Probably best to expose a com object and use them that way

  3. Hi Norman,
    Many thanks - this is also what I was thinking, but where can I find instructions on how to do it?

    How do I "expose a COM object" in C#?

  4. Norman P

    28 Nov 2013 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

    http://msdn.microsoft.com/en-us/library/zsfww439.aspx

  5. Jim C

    28 Nov 2013 Pre-Release Testers Earth
    Edited 6 years ago

    Hi Simon,

    I've seen this question a few times and thought I would respond with an answer.

    Xojo makes this really easy for you. And heres how you do it (quick and easy version):

    • Create a Class Library project in C# (I use VS C# Express 2010).
    • Here is a VERY simple example of a c# project file, with the correct attributes and interfaces:
    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    
    namespace MyXojoToDotNetClassLibrary
    {
       [Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
       [InterfaceType(ComInterfaceType.InterfaceIsDual)]
       public interface IManagedInterface
       {
          [DispId(1)]
          int PrintHi(string name);
       }
    
       [Guid("C6659361-1625-4746-931C-36014B146679")]
       [ClassInterface(ClassInterfaceType.None)]
       public class InterfaceImplementation : IManagedInterface
       {
           public InterfaceImplementation()
           {
    
           }
       
          public int PrintHi(string name)
          {
              MessageBox.Show(name);
             return 1;
          }
       }
    
    }
    • Make sure your target platform is x86
    • check the "Register for Com interop" checkbox in the project build settings. Alternatively you can use the command line tool regasm to register it for you outside of VS (regasm -tlb -codebase MyClassLibrary.dll for example)
    • Make sure you are running Visual Studio as Administrator (or running a command prompt as administrator if you use regasm)
    • build your C# project.
    • Create a new Xojo desktop Project (I'm using 2013.3r3)
    • Go to insert menu, then ActiveX Component, references tab. find and click on the class you made in Visual Studio. In My case, it was called "MyClassLibrary". Xojo will generate a new module and some classes needed to interop with COM.
    • In a button->action event on your Xojo window add the following:
      dim o as new MyClassLibrary.InterfaceImplementation
      Call o.PrintHi("Hello from .NET!!!!!!")

    You should see a message box pop up with your message. You could easily expand this to open .net forms, open xaml forms, interoperate with a database that .NET supports etc.

    There are additional complexities in dealing with events, callbacks and such however this should get you started.

    Have Fun!

  6. Jim C

    28 Nov 2013 Pre-Release Testers Earth

    ugg, cant edit my post.......In my haste to get to my Thanks Giving dinner, a minor error in the C# example above:

    Replace:

    MessageBox.Show("name");

    With:

    MessageBox.Show(name);
  7. Jim C

    28 Nov 2013 Pre-Release Testers Earth

    Ohh, hehe, and one more thing! you will want to use your own GUIDs in any C# code you write. VS can generate them or you can go to http://createguid.com/ to get one.

  8. Russ L

    28 Nov 2013 Pre-Release Testers, Xojo Pro

    That looks cool!
    I didnt know it was possible.

  9. Jim C

    28 Nov 2013 Pre-Release Testers Earth

    Ohh yes! I've used this technique many times in the past to get access to .NET goodies....:)

    There is another way to do this that I haven't explored yet:

    https://www.nuget.org/packages/UnmanagedExports

    Essentially this turns any .NET DLL into something we can call with standard XOJO decalres.......looks interesting.

    Regards,

    Jim

  10. Jim C

    28 Nov 2013 Pre-Release Testers Earth
    Edited 6 years ago

    I think this should serve to tell you how versatile Xojo is. If you are on windows (XP, Windows 7, WIndows 8.x)), you can access anything .net has to offer as well. I've never run into a limitation with Xojo that I couldn't work around in some fashion.

  11. Hi Jim,
    can I use VS C# Express 2012 instead 2010 version ?

    Thanks

    Luciano

  12. Jim C

    29 Nov 2013 Pre-Release Testers Earth

    Luciano,

    VS2012, should work as well.

    Thanks,

    Jim

  13. Jim,
    Just a last question, I would like to create a dll from the ElementFlow control of Fluidkit
    Do you think that is possible ?

    Thanks again

    Luciano

  14. Jim C

    29 Nov 2013 Pre-Release Testers Earth

    Luciano,

    I can't say for sure, but as long as your not going to try to embed one of their controls into a Xojo window, you should be fine. Just create the create the form in c# and show it from there.

  15. Thank you so much Jim! I have been offline for a while, and will try out your example, then try to adapt to my own code.

  16. Finance D

    2 Dec 2013 Pre-Release Testers, Xojo Pro

    I've been looking for something like this (and my Windows Xojo post last week) I make mention about how important it is to get access to the .NET side... Can Paul or someone at Xojo take a look into this to see if there's some documentation that could come out of this for the folks (like myself) who could use this?

  17. Paul L

    2 Dec 2013 Xojo Inc, Third Party Store

    @Gary M Can Paul or someone at Xojo take a look into this to see if there's some documentation that could come out of this for the folks (like myself) who could use this?

    Yes, I will be looking into Jim's example to see if I can get something into the docs. I just have to get Visual Studio set up in a VM. As we all know, it takes a wee bit longer to install Visual Studio than it does to install Xojo!

    Thanks @Jim C !

  18. Finance D

    2 Dec 2013 Pre-Release Testers, Xojo Pro

    Thanks @Paul L!

  19. Finance D

    2 Dec 2013 Pre-Release Testers, Xojo Pro

    I've tested this and it works. I think this is a nice "workaround", but I have to say, COM makes things SLOW.

    We still need a direct call into .NET bindings. I'll look at the NuGet solution as well.

  20. Jim C

    2 Dec 2013 Pre-Release Testers Earth

    All,

    Glad to help my fellow Xojo'ers.

    Gary,

    I'm not sure how well the NuGet solution works with Xojo (I've not had time to check it out). Please let me know what your experience is with it.

    Regards,

    Jim

  21. Newer ›

or Sign Up to reply!