简体   繁体   中英

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)

您应该将用于更新文本框的代码和任何其他特定于UI的代码放入VB6 COM对象中,并将任何非UI代码交给C#类,这样就不必在C#中处理VB6 UI控件。

Are you using .Net 4.0?

You could try using the new dynamic language features:

public void Test(object input)
{
    dynamic textBox = input;
    //Assuming there is a property named Text at runtime
    textBox.Text = "arbitrary string";
}

This should do the equivalent of late binding in COM.

You might also try using dynamic in your method declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM