开发者

C# COM Class - DISP_E_UNKNOWNNAME

开发者 https://www.devze.com 2023-01-15 11:48 出处:网络
I have declared a COM visible class in C#. The code is as follows: [ComVisible(true)] public class AComVisibleClass : TheParentClass

I have declared a COM visible class in C#. The code is as follows:

[ComVisible(true)]
public class AComVisibleClass : TheParentClass
{
    public bool SomeFunc(string id)
    {
        return true;
    }

}

This class is instantiated by a factory class, also COM accessible.

But if I try to access in a VB script file, a DISP_E_UNKNOWNNAME exception is thrown.

This is a new class on a pre-existent library we have here at work. All other classes are accessible through COM. The whole library is compiled into a single assembly file. I have registered the new assembly using regasm, but I still get this exception.

I've tried to debug the COM call using VS2008. The factory class seems to be able to instantiate the AComVisibleClass. The aforementioned exception is thrown only when the factory tries to execute SomeFunc.

I know this may sound a little(?) bit vague, but开发者_JAVA技巧 I cannot expose the real code here. If someone need more information, please ask me.


I can think of three possible reasons for this problem:

Reason 1: Wrong name used in CreateObject:

I suppose that your VBScript code calls something like this:

 Set obj = CreateObject("MyLibrary.AComVisibleClass")

If this is correct, then please open the registry editor and check whether the HKEY_CLASSES_ROOT key contains a subkey called MyLibrary.AComVisibleClass. If it does not, then your library name possibly is different than you expected. Search the registry for AComVisibleClass to find the correct library name.

Reason 2: 64-bit issues:

If the problem happens on a 64-bit operating system, the reason could be that your VBScript runs as a 32-bit process and the C# COM DLL is 64-bit or vice versa.

Reason 3: Wrong function name:

You might be using the wrong function name in the script, e.g. obj.SomeFnc(1) instead of obj.SomeFunc(1), or the function name you have chosen is a reserved keyword in VBScript or it contains unusual characters.


Sounds like you need to support IDispatch.

Check out Does C# .NET support IDispatch late binding?

edit

This answer is likely wrong, and I may yet wind up deleting it. For now, it seems to add value, so I'll let it stay.

0

精彩评论

暂无评论...
验证码 换一张
取 消