Chris McAtackney
Chris McAtackney

Reputation: 5232

Replacing a VB6 DLL called by a CreateDispatch call with a C# Equivalent

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.

Upvotes: 2

Views: 509

Answers (1)

Hans Passant
Hans Passant

Reputation: 942010

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.

Upvotes: 2

Related Questions