Search code examples
c#comvb6com-interop

COM Interop with VB6 and C#


I'm writing a C# COM object that will be used by a VB 6 program. That shouldn't be much of a problem, however, the VB6 call to the COM object passes in a VB Control (in this case a TextBox). The program expects the COM object to alter the Text property of the control. Unfortunately, I have to stick to this interface as I'm dealing with someone elses legacy code.

How can I set the property for the passed in TextBox? Do I simply create an interface with a Text property and cast input to that interface? Is this even possible?

Please let me know if I need to clarify. Note: I intended to leave out the COM declarations in the following code.

// C# COM Object Interface
public interface IObj
{
    // This function must receive the argument of type object.
    void Test(object input);
}

// C# COM Object Implementation
public class Obj : IOjb
{
    // A VB6 TextBox is passed into here,
    // expecting a change to the Text property.
    public void Test(object input)
    {
        // INSERT NECESSARY CODE HERE

        input.Text = "arbitrary string";
    }
}


// VB 6
comObject.Test (txtBox)

Solution

  • You should put code for updating the text box and any other UI specific code in to a VB6 COM object, and hand off any non-UI code to a C# class, then you will not have to deal with a VB6 UI control in C#.