write a com control in c# and use it in MFC

懵懂的女人 提交于 2019-12-11 15:35:32

问题


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


回答1:


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
    }
}

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

regasm.exe /tlb component.dll

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

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

Check this link for more information.




回答2:


This is a bit outside my normal territory, as I don't interact with .NET Interop technology all that much.

It is possible to create what's called a COM Callable Wrapper around your C# control/class to make it accessible to any COM-aware program. I won't duplicate Francis B's answer because it's fairly complete as it stands.

The big question is whether a visual C# control works seamlessly within an MFC window. That's not something I can answer, but my best advice would be to prepare for a bumpy road ahead. Please see http://bytes.com/topic/net/answers/430618-c-control-mfc-window-frame for more detail.



来源:https://stackoverflow.com/questions/1424332/write-a-com-control-in-c-sharp-and-use-it-in-mfc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!