美国机器上的C#互操作失败

本文关键字:互操作 失败 机器 美国 | 更新日期: 2023-09-27 18:28:08

我一直在开发一个可通过com访问的简单dll库,以便其他软件可以使用我们的库(来自任何托管或非托管语言)。

创建一个可访问的dll非常简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MyNamespace
{
    //This interface defines purely the events. DotNetEventSender should implement this interface with the ComSourceInterfaces() attribute
    //to become an Event Source.
    [ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface COMEventsInterface
    {
        //[DispId(1)]
        // we don't have any events, but if needed, include them here
    }
    [ComVisible(true)]
    public interface ICOM
    {
        //Methods
        int Sum(int[] intsToSum)
    }
//Identifies this interfaces that are exposed as COM event sources for the attributed class.
    [ComSourceInterfaces(typeof(COMEventsInterface))]
    //Tells the compiler not to generate an interface automatically and that we are implementing our own interface (IDotNetEventSender)
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class COM : ICOM
    {
        // Methods
        public int Sum(int[] intsToSum)
        {
            int sum = 0;
            foreach ( int i in intsToSum )
            {
                sum += i;
            }
            return sum;
        }
    }
}

在调试模式下,中现在将通过"项目">"属性">"构建">"注册com互操作"将此项目标记为注册com互互操作。

在发布模式下,我有一个安装程序,它将我项目的主要输出标记为"vsdrpCOM"。

这在大多数情况下都很有效。但不知何故,在一些机器上(全是美国人),这是行不通的。com类被注册了,但我经常收到错误:HRESULT 0x80131534,这实际上已经在SO上描述过了:通过经典的ASP 实例化.NET/com互操作类时出错

但事实上,我在这里看不到任何解决方案。我已经检查了用户权限、域权限。。。

编辑:我的真实类的构造函数只做一件事:(我添加了try-catch,因为我在SO上发现这是构造函数中的一个错误…)

// Constructor
    public COM()
    {
        try
        {
            // register itself with the application
            MyApplication.COMObject = this;
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

它只是将自己注册到静态类的属性COMObject:

private static COM _comObject;
public static COM COMObject
        {
            get
            {
                return _comObject;
            }
            set
            {
                _comObject = value;
            }
        }

尽管COM类实际上并不需要注册自己,但我这样做是为了将来使用,如果我想触发Events

美国机器上的C#互操作失败

我碰巧在一个静态类的声明中错误地声明了DateTime。。。private DateTime myDateTime=转换.ToDateTime("2013年9月15日12:00:00");

当然,在欧盟体系中,这是可行的,但在美国人(甚至其他人)身上,这是一个错误,因为没有第15个月。。。

这甚至在我的com可访问类的构造函数之前就被触发了,这就是为什么无法处理错误的原因。

愚蠢的错误,但证明了有时错误看起来很复杂,而它们却很简单。