views:

49

answers:

2

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 I cannot expose the real code here. If someone need more information, please ask me.

A: 

Sounds like you need to support IDispatch.

Check out http://stackoverflow.com/questions/403218/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.

Steven Sudit
.NET exposes dual interfaces (i.e. including IDispatch) by default for ComVisible stuff, so that shouldn't be the issue here.
Pavel Minaev
@Pavel, you're right. It looks like `IDispatch` is implemented but the caller is not passing in the correct name. (http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants.disp_e_unknownname(VS.80).aspx) Could it be case sensitivity?
Steven Sudit
A: 

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.

fmunkert
I was able to find out what was happening. All I can say is that my problem was a little more complex that I described here... BUT this is the most helpful answer, and so will be accepted.
rsenna