在JavaScript中处理ATL / ActiveX事件 [英] Handling ATL/ActiveX events from within JavaScript

查看:132
本文介绍了在JavaScript中处理ATL / ActiveX事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ATL ActiveX控件,引发需要在IE / JavaScript中处理的三个事件(Connected,Authenticated,Disconnected)。据我所知,我做的一切正确,具体来说:



(1)我告诉ATL实现IProviderClassInfo2接口,如此处



(2)请按照此处的说明操作,完成我的ATL项目中的连接点。我现在有一个CProxy_IMyControlEvents类,用适当的Fire_Authenticated(),Fire_Connected()和Fire_Disconnected()实现。他们看起来更像这样:

 模板< class T> 
class CProxy_IVncServerControlEvents:
public ATL :: IConnectionPointImpl< T,& __ uuidof(_IVncServerControlEvents)>
{
public:
HRESULT Fire_Connected()
{
HRESULT hr = S_OK;
T * pThis = static_cast< T *>(this);
int cConnections = m_vec.GetSize();

for(int iConnection = 0; iConnection< cConnections; iConnection ++)
{
pThis-> Lock
CComPtr< IUnknown> punkConnection = m_vec.GetAt(iConnection);
pThis-> Unlock();

IDispatch * pConnection = static_cast< IDispatch *>(punkConnection.p);

if(pConnection)
{
CComVariant varResult;

DISPPARAMS params = {NULL,NULL,0,0};
hr = pConnection-> Invoke(1,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,& params,& varResult,NULL,NULL);
}
}
return hr;
}
};

(3)我在代码中的适当位置调用Fire_Connected主UI线程),并且调试器确认它们正在触发。



(4)我在HTML页面中实例化生成的控件,调用方法就可以了。

 < object id =MyControlclassid =CLSID :42832F4C-3480-4450-A6B5-156B2EFC408Fcodebase =http:// localhost:51150 / Resources / MyControlInstaller.CAB/> 

< script language =javascript>
var myControl = document.getElementById(MyControl);
myControl.connect();
< / script>

(5)我将事件连接到JavaScript函数处理程序。这似乎是我遇到麻烦的地方。我已经看到了三种不同的引用方式,而且没有一种方法适合我。

 < ;! - 方法#1  - > 
< script language =javascript>
myControl.attachEvent('Connected',onConnected);
myControl.attachEvent('Disconnected',onDisconnected);
myControl.attachEvent('Authenticated',onAuthenticated);
< / script>

<! - 方法#2 - >
< script language =javascript>
myControl.Connected = onConnected;
myControl.Disconnected = onDisconnected;
myControl.Authenticated = onAuthenticated;
< / script>

<! - 方法#3 - >
< script for =MyControllanguage =javascript>
function MyControl :: Connected()
{
onConnected();
}

函数MyControl :: Disconnected()
{
onDisconnected();
}

函数MyControl :: Authenticated()
{
onAuthenticated();
}
< / script>

我已经准备好了在互联网上可以找到的关于如何做到的一切,看起来不像我留下任何东西。所以我必须做一些蠢事。关于我可能做错了什么或是如何解决问题的任何建议?

解决方案

我想通过查看Circ样本VS2010附带的项目。原来,(至少在我的情况下)ATL事件向导没有更新分派接口的IDL。生活是好的一旦我更新了IDL文件的dispinterface部分从这里:

  dispinterface _IMyControlEvents 
{
属性:
方法:
};

到此:

  dispinterface _IMyControlEvents 
{
属性:
方法:
[id(1)] void Connected
[id(2)] void Disconnected();
[id(3)] void Authenticated();
};

一旦我这样做,我就能得到各种不同的事件处理语法。 p>

I have an ATL ActiveX control that raises three events (Connected, Authenticated, Disconnected) which need to be handled in IE/JavaScript. So far as I can tell, I'm doing everything right, specifically:

(1) I've told ATL to implement the IProviderClassInfo2 interface, as described here.

(2) I've implemented connection points within my ATL project by following the directions here. I now have a CProxy_IMyControlEvents class, with the appropriate Fire_Authenticated(), Fire_Connected() and Fire_Disconnected() implementations. They look more-or-less like this:

template<class T>
class CProxy_IVncServerControlEvents :
 public ATL::IConnectionPointImpl<T, &__uuidof(_IVncServerControlEvents)>
{
public:
HRESULT Fire_Connected()
 {
  HRESULT hr = S_OK;
  T * pThis = static_cast<T *>(this);
  int cConnections = m_vec.GetSize();

  for (int iConnection = 0; iConnection < cConnections; iConnection++)
  {
   pThis->Lock();
   CComPtr<IUnknown> punkConnection = m_vec.GetAt(iConnection);
   pThis->Unlock();

   IDispatch * pConnection = static_cast<IDispatch *>(punkConnection.p);

   if (pConnection)
   {
    CComVariant varResult;

    DISPPARAMS params = { NULL, NULL, 0, 0 };
    hr = pConnection->Invoke(1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,  &params, &varResult, NULL, NULL);
   }
  }
  return hr;
 }
};

(3) I'm calling Fire_Connected() et al at the appropriate points in my code (on the main UI thread), and the debugger confirms that they're firing.

(4) I'm instantiating the resulting control in an HTML page, and am able to call methods on it. These methods do what they're supposed to do and return successfully.

<object id="MyControl" classid="CLSID:42832F4C-3480-4450-A6B5-156B2EFC408F" codebase="http://localhost:51150/Resources/MyControlInstaller.CAB" />

<script language="javascript">
var myControl = document.getElementById("MyControl");
myControl.connect();
</script>

(5) I'm wiring up the events to JavaScript function handlers. This seems to be where I'm running into trouble. I've seen three different ways of doing it referenced, and none of them seem to be working for me.

<!-- Method #1 -->
<script language="javascript">
myControl.attachEvent('Connected', onConnected);
myControl.attachEvent('Disconnected', onDisconnected);
myControl.attachEvent('Authenticated', onAuthenticated);
</script>

<!-- Method #2 -->
<script language="javascript">
myControl.Connected = onConnected;
myControl.Disconnected = onDisconnected;
myControl.Authenticated = onAuthenticated;
</script>

<!-- Method #3 -->
<script for="MyControl" language="javascript">
function MyControl::Connected()
{
    onConnected();
}

function MyControl::Disconnected()
{
    onDisconnected();
}

function MyControl::Authenticated()
{
    onAuthenticated();
}
</script>

I've ready up on everything I can find on the Internet about how to do this, and it doesn't seem like I'm leaving anything out. So I must be doing something stupid. Any suggestions about what I might be doing wrong or how to troubleshoot it?

解决方案

I figured it out by looking at the Circ sample project that comes with VS2010. Turns out that (at least in my case) the ATL event wizard didn't update the IDL for the dispatch interface. Life was good once I updated the dispinterface section of the IDL file from this:

dispinterface _IMyControlEvents
{
    properties:
    methods:
};

To this:

dispinterface _IMyControlEvents
{
    properties:
    methods:
        [id(1)] void Connected();
        [id(2)] void Disconnected();
        [id(3)] void Authenticated();
};

Once I did that, I was able to get the various different event handling syntaxes to work.

这篇关于在JavaScript中处理ATL / ActiveX事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆