Registering my .net assembly for COM interoperabil

2019-02-15 05:36发布

Ok, I'm going crazy. I've been trying to make a simple .net assembly visible to use as a COM object but it's not working. I've been trying a lot of different aproaches but none of them work. So far the closest i've got is the one with the least amount of attributes, interfaces, manually registering etc.

I have a vbscript as a test which looks like this:

Dim o
o = CreateObject("MyNamespace.MyClass")
msgbox o.Test()

My c# code looks like this:

using System;
using System.Runtime.InteropServices;
namespace MyNamespace 
{
  [ProgId("MyNamespace.MyClass")]
  public class MyClass
  {
    public string Test()
    {
       return "teststring";
    }
  }
}

On my project (VS2008) i've selected 'Make assembly COM visible' - which added an attribute to my AssemblyInfo.cs and i also checked 'Register for COM interop'.

I Can see that the ProgId really makes no difference (in my case anyways) - as long as i specify the real namespace+classname, i can however override it which i guess is nice.

The problem is - my CreateObject goes well, but the method Test() can not be found. In fact if i take the dll and extract the tlb and expect that i see no methods at all.

The error i get is this:

C:\inetpub\wwwroot\ASPTest\testrun.vbs(3, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'Test'

I'd love any feedback, but just to let you know what i've been trying. I've messing around with Guid and AttributeGuid on the class, defining the COMVisible explicitly on the class as well i tried NOT registering for COM interop and instead did regasm /codebase /regfile.

One last note - most my testing has revolved around a non-signed assembly, but trying to pinpoint the error i tried with a strongname/key as well but it didn't do anything.

Can anyone help me find the problem, please?

6条回答
我想做一个坏孩纸
2楼-- · 2019-02-15 06:00

I started from the beginning and removed anything regarding COM interop on my project, i then added

[Guid(...some guid...)]
[ComVisible(true)]

on MyClass and i added a strongname (used the project setting to set it - instead of defining it in the AssemblyInfo.cs.

When i compiled the assembly, i copied the files to another location where i did a

regasm MyNamespace.MyClass.dll /codebase /regfile

Then i ran and accepted the registry entry.

This made it work - whenever i make changes i just recompile and copy the files over the old (no unregistering) and then i only do:

regasm MyNamespace.MyClass.dll /codebase

... and that's it - changes i made to MyClass are imediately visible in my vbscript and in my asp page.

查看更多
男人必须洒脱
3楼-- · 2019-02-15 06:02

Did i mention i run Windows 7 RC 64 bit?

Well i think that might explain my VS isn't doing it right: http://kbalertz.com/956933/Issues-building-project-assembly.aspx It seems VS uses the 32bit version of registry and not the 64bit...

查看更多
男人必须洒脱
4楼-- · 2019-02-15 06:03

check this link it work perfectly I follow it and gives perfect solution over the error like "Object doesn't support this property or method" Code Project Help

查看更多
聊天终结者
5楼-- · 2019-02-15 06:07

Don't you need to define some interface that has the methods, and implement that interface?

查看更多
做个烂人
6楼-- · 2019-02-15 06:08

This works for me:

using System;
using System.Runtime.InteropServices;
namespace MyNamespace
{

    [GuidAttribute("8DDE02DF-B31C-4d91-98A7-C64444701985")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class clsCOMWraper
    {
       public string Test()
      {
         return "teststring";
      }
    }
}

Thse assembly must be signed.

The, register with regasm /codebase

查看更多
Luminary・发光体
7楼-- · 2019-02-15 06:25

I dont see any constructor for the Class. COM Interop needs a default constructor (without any params) for it to generate a COM interface successfully.

Try adding a constructor and see if it works.

查看更多
登录 后发表回答