【问题标题】:How to solve case insensitive conflict name in COM while I using a case sensitive language如何在我使用区分大小写的语言时解决 COM 中不区分大小写的冲突名称
【发布时间】:2013-03-23 11:04:40
【问题描述】:

这个问题是this article 的扩展。

在同样的情况下,我通过它的ProgID 创建了一个 WMP ActiveX 实例。

protected const string WMP_PROG_ID = "WMPlayer.OCX.7";

private dynamic _wmp;

protected virtual bool init(){
    try{
        _wmp = Activator.CreateInstance(Type.GetTypeFromProgID(WMP_PROG_ID));
    }
    catch{ return false; }
    return connectEvent();
}

根据MSDN 文档,WMPlayer 对象中有一个Error 事件和一个error 属性。 所以,我尝试以这种方式附加事件。

protected bool connectEvent(){
    try{
        _wmp.PlayStateChange += new StateHandler(_wmp_PlayStateChange);
        //_wmp.Error += new Action(_wmp_ErrorEvent);
    }
    catch { return false; }
    return true;
}

protected void _wmp_PlayStateChange(WMPlayerState state){
    //do something I like
}

protected void _wmp_ErrorEvent(){
    //do some error handling
}

如果我继续评论//_wmp.Error += new Action(_wmp_ErrorEvent), 没有编译错误,PlayStateChange 工作得很好。

但是,如果我删除注释标记,则会出现运行时异常。 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: can not apply operator "+=" between 'System.__ComObject' and 'System.Action'

这两个“错误”似乎是冲突的,因为 COM 不区分大小写。我该如何解决? 我的目标是在不使用 AxWindowsMediaPlayer 的情况下附加到“错误”事件。

【问题讨论】:

    标签: c# com case-sensitive case-insensitive wmp


    【解决方案1】:

    我遇到了和你非常相似的问题,但我的问题是Size。强类型 ActiveX 控件重新定义了 Size,因此当表单设计者想要调整表单上的控件大小时,我需要将其转换回 Control

    ((Control)this.axLEAD1).Size = new System.Drawing.Size(298, 240);
    

    如果您可以获得 com 对象的强类型类(通过添加引用或使用 tlbimp.exe),您可以强制转换为强类型 com 对象而不是 __ComObject 并让它使用正确的方法.

    【讨论】:

    • 感谢您的想法,我尝试使用 tlbimp 转换 wmp.dll,并对其进行反汇编。但是,只有 Error 属性,但没有 error 事件。我猜MS工具的转换也不能处理这个冲突问题。
    猜你喜欢
    • 2016-11-24
    • 2013-03-06
    • 2011-10-14
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2020-02-18
    相关资源
    最近更新 更多