1

I have created a .Net DLL with few simple classes. I have registered the DLL using RegAsm and I got a message that types were registered successfully.

RegAsm Syntax Used :

C:\Windows\Microsoft.NET\Framework\v4.0.30319>RegAsm.exe "D:\Projects\TestDLL.Core.dll"

C# Code:

namespace MyTestDLL.Core    
    {  
        public class PacketInfo    
        {    
          // constructor
          public PacketInfo()
          {
          }

          public string HostName { get; set; }
            //   and so on ......

        }    
    }

I have set the ComVisible property to true in AssemblyInfo.cs file of this DLL. // [assembly: ComVisible(true)]

However when I create an object out of it in JavaScript and run the script in Command prompt , I'm getting either it is not an object or null.

JS Code :

 var testObj = new ActiveXObject(MyTestDLL.Core.PacketInfo);
        testObj.HostName = "Test";

Can anyone advise me how to resolve this ?

Vikineese
  • 61
  • 1
  • 8
  • There is a question in Stack Overflow that offers a solution to this question: http://stackoverflow.com/questions/858140/how-do-i-call-a-method-in-a-custom-activex-dll-using-java-vb-script – TejSoft Feb 25 '15 at 10:53

2 Answers2

1

You need to add a ProgId attribute to the class, something like this:

[Guid("some guid that you will never change")]
[ProgId("MyLib.PacketInfo")] // this is arbitrary
public class PacketInfo    
{
    ....
}

The guid is optional, but if you don't set it yourself, it will be something you won't control, so it's better to define it. And of course I did not add the ComVisible because you already defined it at assembly level (I personnally prefer to set it at class level).

Then, once registered, you shall be able to use it like this (use the progid as a string):

var testObj = new ActiveXObject('MyLib.PacketInfo');
testObj.HostName = "Test";
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Thanks for your comment. Let me try this and update you. – Vikineese Feb 25 '15 at 12:15
  • That didn't work as expected. Still I get the same error when I run the js.. either myObj is null or not an object. I did exactly what you have suggested. Even added COM Visible attribute at the class level itself. – Vikineese Feb 25 '15 at 14:24
  • There can be a lot of other issues. You need to run regasm with the /codebase argument, and you also need to make sure your javascript runs with the same bitness as the assembly (both x86 or both x64). – Simon Mourier Feb 25 '15 at 22:11
-2

I was able to achieve that by adding the following line to My DLL just above the class,

 [Guid("A32AB771-9967-4604-B193-57FAA25557D4"), ClassInterface(ClassInterfaceType.None)] 

Earlier I wasn't having the ClassInterfaceType part in my code. Also ensure that each of your classes are having unique GUID. FYI : You can create GUID using Visual Studio Tools GUID Creator GUI.

Vikineese
  • 61
  • 1
  • 8