连接到C#中的COM事件 - 支持受管和非受管服务器 [英] Connect to COM events in C# - support both managed and unmanaged servers
问题描述
我正在编写需要连接到COM事件的C#代码。我实现了使用
IConnectionPointContainer和IConnectionPoint,因此:
I'm writing C# code that needs to connect to COM events. I implemented the use of IConnectionPointContainer and IConnectionPoint thus:
IConnectionPointContainer connectionPointContainer = internalGenerator as IConnectionPointContainer;
if (connectionPointContainer == null)
{
Debug.Fail("The script generator doesn't support the required interface - IConnectionPointContainer");
throw new InvalidCastException("The script generator doesn't support the required interface - IConnectionPointContainer");
}
Guid IID_IScriptGeneratorEvents = typeof(IScriptGeneratorCallback).GUID;
connectionPointContainer.FindConnectionPoint(ref IID_IScriptGeneratorEvents, out m_connectionPoint);
m_connectionPoint.Advise(this, out m_cookie);
$ b < .Net创建它,它将它作为.Net对象而不是COM对象处理。由于.Net对象没有实现IConnectionPointContainer接口,当试图将对象转换到该接口时,我会得到null。
The problem is that when the COM server is actually implemented in .Net (say, C#), after .Net creates it, it handles it as a .Net object, not a COM object. Since the .Net object doesn't implement the IConnectionPointContainer interface, I get null when trying to cast the object to that interface.
任何想法如何解决这个问题?
我当然可以在C#COM服务器中实现IConnectionPointContainer,但是我想要一个更简单的解决方案,我可以很容易地解释给需要实现COM服务器的其他开发人员。
Any idea how can i workaround this? I can of course implement IConnectionPointContainer by myself in the C# COM server, however I would like a simpler solution, which I can easily explain to other developers which need to implement the COM server.
PS我必须使用IConnectionPointContainer作为COM服务器可以在非.NET(C ++,Java)中实现。
P.S I must use IConnectionPointContainer as the COM server may be implemented in non-.Net (C++, Java).
谢谢,
Inbar
Thanks, Inbar
推荐答案
我没有找到办法。
最后,我将在.Net中定义另一个接口,并将编写2个代码路径,一个用于.Net对象,一个用于真正的COM对象。
I didn't find a way to do this. Eventually I will define another interface in .Net and will write 2 code paths, one for .Net objects and one for real COM objects.
这篇关于连接到C#中的COM事件 - 支持受管和非受管服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!