Shoutbox

Using C# DLL - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Using C# DLL (/showthread.php?tid=95643)

Using C# DLL by barby850717@gmail. on 10-20-2010 at 08:48 PM

Hi,

I have a DLL that I did using C# and I would like to use it from my script.

I have already tried the Interop.Call method but It does not work.

Can someone please point out how can this be done ? I mean, how can I use the DLL in my script.

Thanks in advance,


RE: Using C# DLL by Matti on 10-20-2010 at 09:15 PM

Interaction with C# DLLs should be done with COM, not with Interop.Call. You need to make your classes and methods "ComVisible" so that they are exposed for COM in the DLL. Have a look at trying to create a c# COM server.

If you want your C# project to interact with the Plus! objects (Messenger, MsgPlus,...), see if you can find something in this Tutorial on How to Let a Script-Callable DLL Handle Scripting Objects. It's written for C++, but you should be able to do the same in C# as well.


RE: Using C# DLL by barby850717@gmail. on 10-20-2010 at 09:26 PM

Hi, I tested what you suggested but I get the following error:

Automation server can't create object

And my ActiveX Object does not get created.

Any other suggestion?


RE: Using C# DLL by CookieRevised on 10-21-2010 at 08:21 AM

Have you registered the DLL first?

Also what Matti said:

quote:
Originally posted by Matti
Interaction with C# DLLs should be done with COM, not with Interop.Call. You need to make your classes and methods "ComVisible" so that they are exposed for COM in the DLL.

Thus, not any .NET DLL can be called though. It needs to be properly prepped to handle COM.

Example tutorial (although the example calling script is VBScript, the exact same things for the DLL apply for when you want to call it from JScript):
http://www.codeproject.com/Articles/79314/How-to-...om-a-VBScript.aspx
Also read the comments in that article, especially "Improovements for your code"




RE: Using C# DLL by barby850717@gmail. on 10-21-2010 at 01:32 PM

Hi, I created a new class just like the VB sample code.

using System;
using System.Runtime.InteropServices;
namespace MyDLL
{
    [ComVisible(true)]
    public class Operations
    {
        [ComVisible(true)]
        public string getValue1(string sParameter)
        {
            switch (sParameter)
            {
                case "a":
                    return "A was chosen";

                case "b":
                    return "B was chosen";

                case "c":
                    return "C was chosen";

                default:
                    return "Other";
            }
        }
        public string getValue2()
        {
            return "From VBS String Function";
        }
    }
}

And the I try to use it from jscript.

I have tried:
Interop.Call("C:\\Program Files\\Messenger Plus! Live\\Scripts\\MySecondScript\\MyDLL.dll", "getValue1", "a");
and I received the error: Interop.Call failed to locate function "getValue1"

I have also tried: var myObj = new ActiveXObject("MyDLL.Operations"); and I receive the error: unknown (code: -2147024894)


RE: Using C# DLL by matty on 10-21-2010 at 10:27 PM

Did you regasm the dll to register it?


RE: Using C# DLL by felipEx on 10-22-2010 at 02:33 AM

quote:
COM classes must have a parameterless Public Sub New() constructor, or the class will not register correctly
Source: Walkthrough: Creating COM Objects with Visual Basic .NET

:)
RE: RE: Using C# DLL by CookieRevised on 10-22-2010 at 07:12 AM

quote:
Originally posted by felipEx
quote:
COM classes must have a parameterless Public Sub New() constructor, or the class will not register correctly
Walkthrough: Creating COM Objects with Visual Basic .NET
That only applies for VB.NET (right?).

He is using C#...
RE: RE: RE: Using C# DLL by felipEx on 10-23-2010 at 05:15 AM

quote:
Originally posted by CookieRevised
quote:
Originally posted by felipEx
quote:
COM classes must have a parameterless Public Sub New() constructor, or the class will not register correctly
Walkthrough: Creating COM Objects with Visual Basic .NET
That only applies for VB.NET (right?).

He is using C#...
It doesn't matter at all if it's C#/VB.NET, COM classes must have a parameterless constructor in order to register correctly. Sorry if I wasn't clear in the first place ;-)

Just made a quick test and it's working:
Spoiler:

C# code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
 
namespace MyDLL
{
    [ComVisible(true)]
    public class Operations
    {
        /// <summary>
        /// Parameterless constructor required in order to register correctly
        /// </summary>
        public Operations()
        {
        }
 
        public string getValue1(string sParameter)
        {
            switch (sParameter)
            {
                case "a":
                    return "A was chosen";
 
                case "b":
                    return "B was chosen";
 
                case "c":
                    return "C was chosen";
 
                default:
                    return "Other";
            }
        }
        public string getValue2()
        {
            return "From VBS String Function";
        }
    }
}

JScript code:
var x = new ActiveXObject("MyDLL.Operations");
Debug.Trace(x.getValue2());
x = null;

quote:
Originally posted by output
Script is starting
Script is now loaded and ready
Function called: OnEvent_Initialize
From VBS String Function

RE: Using C# DLL by CookieRevised on 10-23-2010 at 08:27 AM

quote:
Originally posted by felipEx
quote:
Originally posted by CookieRevised
quote:
Originally posted by felipEx
quote:
COM classes must have a parameterless Public Sub New() constructor, or the class will not register correctly
Source: Walkthrough: Creating COM Objects with Visual Basic .NET
That only applies for VB.NET (right?).

He is using C#...
It doesn't matter at all if it's C#/VB.NET, COM classes must have a parameterless constructor in order to register correctly. Sorry if I wasn't clear in the first place ;-)
ah yeah, but that's not what I meant. The code you showed for the parameterless constuctor (underlined bit) is VB.NET, not C#...

;)

Also, in the C# code I showed, which is a working code apparently, they don't have that parameterless constructor, yet it does register perfectly according to the article though, so what are they doing differently?

Either way, also according to the code I showed, you best add a few more things to it in order to make it good COM Interop.

RE: Using C# DLL by SmokingCookie on 10-28-2010 at 06:02 PM

There's a .NET class for exporting C# methods; the only problem I have with it, is that it doesn't work *-)