如何在C#中创建ActiveX控件(COM)? [英] How do I create an ActiveX control (COM) in C#?

查看:124
本文介绍了如何在C#中创建ActiveX控件(COM)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个ActiveX控件。我使用视觉工作室  2010 (.NET 4)。我需要创建 COM 对象(在C#中),并且不知道如何开始使用要使用的项目类型等)

解决方案

OK我找到了解决方案,


  1. 以管理员身份启动VS2010。

  2. 打开类库项目(exmaple - MyProject)。

  3. 向项目添加新界面(请参见下面的示例)。

  4. 使用System.Runtime.InteropServices; 添加到文件
  5. 添加属性InterfaceType,Guid到接口。

  6. 您可以使用工具 - >生成GUID(选项4)生成Guid。

  7. 添加实现接口的类。

  8. 将属性ClassInterface,Guid和ProgId添加到界面。

    ProgId约定是{namespace}。{class}

  9. 在AssemblyInfo文件中项目的Properties文件夹下将ComVisible设置为true。

  10. 在项目属性菜单中,在构建选项卡中标记注册COM互操作
  11. 构建项目



例如:
C#代码:



现在你可以使用它的ProgID来使用你的COM对象。 p>

 使用System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

使用System.Runtime.InteropServices;

namespace Launcher
{

[InterfaceType(ComInterfaceType.InterfaceIsDual),Guid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)]
public interface ILauncher
{
void launch();
}

[ClassInterface(ClassInterfaceType.None),Guid(YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY),ProgId(Launcher.Launcher)]
public class Launcher:ILauncher
{
private string path = null;

public void launch()
{
Console.WriteLine(I launch scripts for a living。

}

}
}

和VB脚本使用COM:




set obj = createObject(PSLauncher.PSLauncher)
obj .launch()



,输出将是:



我为生活启动脚本


I am trying to create an ActiveX control. I using Visual Studio 2010 (.NET 4). I need to create a COM object (in C#) and have no idea how to get started (what type of project to use,etc.)

解决方案

OK I found the solution and I'll write it here for the common good.

  1. Start VS2010 as administrator.
  2. Open a class library project (exmaple - MyProject).
  3. Add a new interface to the project (see example below).
  4. Add a using System.Runtime.InteropServices; to the file
  5. Add the attributes InterfaceType, Guid to the interface.
  6. You can generate a Guid using Tools->Generate GUID (option 4).
  7. Add a class that implement the interface.
  8. Add the attributes ClassInterface, Guid, ProgId to the interface.
    ProgId convention is {namespace}.{class}
  9. Under the Properties folder in the project in the AssemblyInfo file set ComVisible to true.
  10. In the project properties menu, in the build tab mark "Register for COM interop"
  11. Build the project

now you can use your COM object by using it's ProgID.

example: the C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;

namespace Launcher
{

    [InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
    public interface ILauncher
    {
        void launch();
    }

    [ClassInterface(ClassInterfaceType.None), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY"), ProgId("Launcher.Launcher")]
    public class Launcher : ILauncher
    {
        private string path = null;

        public void launch()
        {
            Console.WriteLine("I launch scripts for a living.");

        }

    }
}

and VB script using the COM:

set obj = createObject("PSLauncher.PSLauncher") obj.launch()

and the output will be:

I launch scripts for a living

这篇关于如何在C#中创建ActiveX控件(COM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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