处理JavaScript内的ATL / ActiveX事件 [英] Handling ATL/ActiveX events from within JavaScript

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

问题描述

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



(1)我已经告诉ATL实现IProviderClassInfo2接口,如 here



(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(); (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>
函数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天全站免登陆