Using C# DLL |
Author: |
Message: |
barby850717@gmail.
New Member
Posts: 9
Joined: Oct 2010
|
O.P. Using C# DLL
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,
|
|
10-20-2010 08:48 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: Using C# DLL
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.
|
|
10-20-2010 09:15 PM |
|
|
barby850717@gmail.
New Member
Posts: 9
Joined: Oct 2010
|
O.P. RE: Using C# DLL
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?
|
|
10-20-2010 09:26 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: Using C# DLL
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"
This post was edited on 10-22-2010 at 07:15 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
10-21-2010 08:21 AM |
|
|
barby850717@gmail.
New Member
Posts: 9
Joined: Oct 2010
|
O.P. RE: Using C# DLL
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)
|
|
10-21-2010 01:32 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: Using C# DLL
Did you regasm the dll to register it?
|
|
10-21-2010 10:27 PM |
|
|
felipEx
Scripting Contest Winner
Posts: 378 Reputation: 24
35 / /
Joined: Jun 2006
|
|
10-22-2010 02:33 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: RE: Using C# DLL
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#...
This post was edited on 10-22-2010 at 07:13 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
10-22-2010 07:12 AM |
|
|
felipEx
Scripting Contest Winner
Posts: 378 Reputation: 24
35 / /
Joined: Jun 2006
|
RE: RE: RE: Using C# DLL
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
|
|
10-23-2010 05:15 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: Using C# DLL
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.
This post was edited on 10-23-2010 at 09:33 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
10-23-2010 08:27 AM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|