当前位置 > 首页 > IE9\IE8

在IE9中以IDispatchEx::InvokeEx方式回调JS函数无反应

2012-5-15 15:42:00来源:IE9\IE8

在一个线程通过IDispatchEx::InvokeEx回调一个JS方法,结果JS无反应。

//
STDMETHODIMP CMyClass::FireOnStateChanged()
{
	//触发脚本
	if (this->m_OnStateChanged)
	{
		VARIANT var;
		DISPPARAMS dispparams; 
		DISPID dispid = 0;
		memset(&dispparams, 0, sizeof dispparams); 
		dispparams.cArgs      = 2;
		dispparams.rgvarg     = new VARIANT[dispparams.cArgs]; 
		dispparams.cNamedArgs = 0; 
		//[0]obj [1]state
		dispparams.rgvarg[1].pdispVal = this->m_Object;
		dispparams.rgvarg[1].vt = VT_DISPATCH;
		dispparams.rgvarg[0].uintVal = this->m_State;
		dispparams.rgvarg[0].vt = VT_UINT;//以下代码在IE8,IE7,IE6中能够正常调用,在IE9中无反应
		this->m_OnStateChanged->InvokeEx(dispid,
			LOCALE_USER_DEFAULT, 
			DISPATCH_METHOD, 
			&dispparams, 
			&var, 
			NULL, 
			NULL);
	}
	return S_OK;
}
//线程函数
void CMyClass::ThreadFunction(int taskID)
{
	this->m_State = HUS_UploadComplete;//更改状态
	//通过脚本调用
	if (this->m_OnStateChanged)
	{
		CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
		IHttpUploader* atl = NULL;
		if (FAILED(pGIT->GetInterfaceFromGlobal(gdwCookie, IID_IHttpUploader, (LPVOID*)&atl)))
		{
			MessageBoxW(NULL,L"从全局GIT中获取指针失败",L"OnUploadOk",MB_OK);
			return;
		}
		atl->FireOnStateChanged();//通知客户传输完毕
		::CoUninitialize();
	}
}
//类成员变量
IDispatchEx* m_OnStateChanged;
//启动线程函数
	IMyClass* p = (IMyClass*)this;//
	// 创建全局接口表 Global Interface Table(GIT)
	if (pGIT) pGIT->Release();//fix:出现错误后重新传输时会有泄露
	HRESULT hRes = CoCreateInstance(CLSID_StdGlobalInterfaceTable, 
		NULL, 
		CLSCTX_INPROC_SERVER,
		IID_IGlobalInterfaceTable, 
		(LPVOID*)&pGIT);
	// 将接口保存到全局接口表store interface in GIT
	if (FAILED(pGIT->RegisterInterfaceInGlobal(p, IID_IHttpUploader, &gdwCookie)))
	{
		MessageBoxW(NULL,L"创建全局GIT失败",L"000",MB_OK);
		return ;
	}
	_ASSERT(SUCCEEDED(hRes));
//启动线程
	this->m_HThread = (HANDLE)_beginthreadex(NULL,0,ThreadFunction,this,0,&threadID );