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 ?

1

2 Answers 2

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";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your comment. Let me try this and update you.
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.
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).
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.