简体   繁体   English

当我使用区分大小写的语言时,如何解决COM中不区分大小写的冲突名称

[英]How to solve case insensitive conflict name in COM while I using a case sensitive language

This question is an extension of this article . 这个问题是本文的扩展。

In the same case, I created a instance of WMP ActiveX by its ProgID . 在同一情况下,我通过其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();
}

According the MSDN document, there are an Error event and an error property in WMPlayer object. 根据MSDN文档,WMPlayer对象中有一个Error事件和一个error属性。 So, I try to attach events like this way. 因此,我尝试以这种方式附加事件。

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
}

If I keep //_wmp.Error += new Action(_wmp_ErrorEvent) commented, there's no compile error and PlayStateChange works pretty good. 如果我保持//_wmp.Error += new Action(_wmp_ErrorEvent)注释,则不会发生编译错误,并且PlayStateChange效果很好。

However, if I remove the comment mark, there's a runtime exception. 但是,如果删除注释标记,则会出现运行时异常。 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: can not apply operator "+=" between 'System.__ComObject' and 'System.Action'

It seems the two "error" are conflicted because COM is case insensitive. 似乎两个“错误”是冲突的,因为COM不区分大小写。 How can I solve it? 我该如何解决? My goal is that attach to "Error" event without using AxWindowsMediaPlayer. 我的目标是不使用AxWindowsMediaPlayer附加到“错误”事件。

I ran in to a very similar problem as you but mine was with Size . 我遇到了一个与您非常相似的问题,但是我遇到的是Size The strongly typed ActiveX control redefined Size so I needed to cast it back to Control when the forms designer wanted to size the control on my form. 强类型的ActiveX控件重新定义了Size因此当表单设计人员想要在表单上调整Control Size ,我需要将其强制转换回Control

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

If you can get a strongly typed class for the com object (by adding a reference or using tlbimp.exe ) you may be able to cast to the strongly typed com object instead of __ComObject and have it use the correct method. 如果可以获得com对象的强类型类(通过添加引用或使用tlbimp.exe ),则可以将其转换为强类型com对象而不是__ComObject ,并使其使用正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM