COM Interop与VB6和C# [英] COM Interop with VB6 and C#

查看:126
本文介绍了COM Interop与VB6和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将被VB 6程序使用的C#COM对象。这不应该是一个问题,但是,VB6调用COM对象传递一个VB控件(在这种情况下是一个TextBox)。程序期望COM对象更改控件的Text属性。不幸的是,我必须坚持这个接口,因为我正在处理一个人遗留的代码。

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.

如何设置传递的TextBox的属性?我是否只需创建一个具有Text属性的接口并将输入转换到该接口?这是可能吗?

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?

如果我需要澄清,请让我知道。注意:我打算在以下代码中省略COM声明。

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控件。

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#.

这篇关于COM Interop与VB6和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆