views:

28

answers:

1

An existing Visual C++ application makes the following call;

BOOL bRet = pMyClass.CreateDispatch("BlahBlah.MyClass");
if ( !bRet )
{
    // Error handling snipped
}
else
{
    pMyClass.MyMethod();
    pMyClass.ReleaseDispatch();    
}

pMyClass is a class which was apparently auto-generated by ClassWizard, and it inherits from COleDispatchDriver.

The actual DLL to which it refers is a VB6 one, and this is being migrated to C# as part of an effort to move away from VB in general.

My question is... is there anything special I need to do to make sure that the C# assembly will work in the same way as the original VB6 module did? Currently, the C# looks like this;

[ComVisible(true)]
[ProgId("BlahBlah.MyClass")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass
{
    ...

    public void MyMethod()
    {
        ...
    }
}

Is this sufficient? Are there any gotchas to be aware of when setting public string fields (not shown in code) on MyClass?

Note that I'm not the original author of this code - it's from a legacy system and I'm just doing the migration.

+1  A: 

The CreateDispatch call uses late binding to talk to the COM server. ClassInterfaceType.AutoDispatch. Using AutoDual is fine, that also includes late binding support. With the significant advantage that you can make it a lot faster some day. Late binding isn't cheap.

Hans Passant
Excuse the newbie question, but I've never really worked with COM before. I don't see this particular assembly under Component Services, but I have found a key for it under HKEY_CLASSES_ROOT\CLSID\. Does this mean that it was registered with regsvr32? And if so, what is the C#.NET equivalent of making the new assembly visible to CreateDispatch in the same way?
C.McAtackney
Yes, this is normal. You use Regasm.exe for a .NET assembly instead of Regsvr32.exe. It is automatic if you tick Project + Properties, Build tab, "Register for COM interop".
Hans Passant
I've read about needing a Type Library - do I need to manually embed this as a resource in my .NET assembly? Or does "Register for COM Interop" do this automatically?
C.McAtackney
Sorry, this isn't a subscription service. I see no evidence that you considered my attempts at helping you to be helpful. You can get better help by putting a bounty on your question.
Hans Passant
Apologies, I should have made that last part a new question. Thanks for your help - you have pointed me in the right direction and it's appreciated.
C.McAtackney