在C#中编写一个com控件,并在MFC中使用它 [英] write a com control in c# and use it in MFC

查看:97
本文介绍了在C#中编写一个com控件,并在MFC中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在C#中编写com控件或Activex并在MFC中使用它?

if this possible to write a com control or activex in C# and use it in MFC ?

推荐答案

是.首先,您需要创建COM对象.下面是一个非常简单的示例.

Yes. First, you need to create COM object. Below is a very simple example.

[Guid("123565C4-C5FA-4512-A560-1D47F9FDFA20")]
public interface IDoSomething
{
    [DispId(1)]
    string Name { get; }

    [DispId(2)]
    int DoSomething();
}

[ComVisible(true)]
[Guid("12AC8095-BD27-4de8-A30B-991940666927")]
[ClassInterface(ClassInterfaceType.None)]
public sealed class DoSomething: IDoSomething
{
    public DoSomething()
    {
    }

    public string Name
    {
        get { return ""; }
    }

    public int DoSomething()
    {
        return 4; //random number
    }
}

在那之后,您需要重新组装您的组件. regasm工具将添加必要的注册表COM条目:

After that you need to regasm your assembly. The regasm tool will add the necessary registry COM entries:

regasm.exe /tlb component.dll

/tlb是生成要在MFC应用程序中导入的类型库所必需的.

/tlb is necessary to generate the type library to be imported in your MFC application.

注册程序集后,就可以像其他COM对象一样在MFC应用程序中调用DoSomething.

Once your assembly is registered, you can call DoSomething in your MFC application like any other COM objects.

请点击此链接以获取更多信息.

Check this link for more information.

这篇关于在C#中编写一个com控件,并在MFC中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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