更改COM类的ProgID

时间:2014-07-22 09:11:46

标签: c# com progid

我正在用C#创建一个COM组件。安装时,其ProgID表示为其<Namespace>.<Classname>。但我想将其更改为<Vendor>.<ClassName>.<VersionNumber>

如何在C#中执行此操作。我正在使用Visual Studio 2010。

3 个答案:

答案 0 :(得分:3)

MS声明:

  

ProgId是通过组合以下内容自动为类生成的   命名空间,其类型名称以点号分隔。

这是不正确的,因为根据我的经验,ProgId是由项目名称和类组合而成的。由于默认名称空间是项目的名称,因此MS的声明似乎是正确的,但是如果更改名称空间的名称,则ProgId不会相应更改。

MS继续:

  

这可能会产生无效的ProgId,因为ProgId仅限于   39个字符,除点号[I]外不得包含其他标点符号   认为:只有一个时期]。在这种情况下,可以手动设置ProgId   使用ProgId属性分配给该类。

因此,在我看来,您只能在这种情况下更改ProgId,在通常情况下,将ProgId设置为无用时,它将始终为ProjectName.ClassName。

在下面的示例中,我通过选择Dietrich.Math作为项目名称来尝试了Dietrich.Math.ClassName的ProgId,但没有成功:Dietrich.Math更改为Dietrich_Math。如预期的那样,.NET将忽略ProgId属性,而ProgId仍设置为Dietrich_Math.Arithmetic。

using System;
using System.Runtime.InteropServices;

namespace Dietrich.Math
{
  [ComVisible(true), Guid("B452A43E-7D62-4F11-907A-E2132655BF97")]
  [InterfaceType(ComInterfaceType.InterfaceIsDual)]
  public interface IArithmetic
  {
    int Add(int a, int b);
  }

  [ComVisible(true), Guid("17A76BDC-55B7-4647-9465-3D7D088FA932")]
  [ProgId("SimpleMath.Whatever")]
  [ClassInterface(ClassInterfaceType.None)]
  public class Arithmetic : IArithmetic
  {
    public int Add(int a, int b) { return a + b; }
  }
}

答案 1 :(得分:2)

您是否尝试使用ProgId属性?

答案 2 :(得分:-1)

我认为用户@Bond有正确的想法。不幸的是@Bond没有留下一个例子。以下是使用ProgId ...

的示例
using System;
using System.Runtime.InteropServices;

namespace EncryptionCOMTool
{
    [ComVisible(visibility:true)]
    [Guid(guid: "4a69e3ce-7cf8-4985-9b1a-def7977a95e7")]
    [ProgId(progId: "EncryptionCOMTool.EncryptDecrypt")]
    [ClassInterface(classInterfaceType: ClassInterfaceType.None)]
    public class EncryptDecrypt
    {
        public EncryptDecrypt()
        {

        }

        public string Encrypt(string input)
        {
            return "some encrypted value";
        }

        public string Decrypt(string input)
        {
            return "some decrypted value";
        }
    }
}

由于属性ProgId需要输入字符串,因此您可以在此处放置任何内容,包括供应商名称。对于代码维护,您可以选择将ProgId与namespace.class名称保持一致。为此,请使用您需要的供应商名称来更改类的名称空间以包含供应商名称,并且为了完整性,还要更改项目属性中的默认名称空间。

相关问题