Trouble in ActiveX multi-thread invoke javascript callback routine
Posted
by
code0tt
on Stack Overflow
See other posts from Stack Overflow
or by code0tt
Published on 2011-11-15T09:47:38Z
Indexed on
2011/11/15
9:50 UTC
Read the original article
Hit count: 281
everyone. I'm get some trouble in ActiveX programming with ATL. I try to make a activex which can async-download files from http server to local folder and after download it will invoke javascript callback function. My solution: run a thread M to monitor download thread D, when D is finish the job, M is going to terminal themself and invoke IDispatch inferface to call javascript function. **************** THERE IS MY CODE: ****************
/* javascript code */
funciton download() {
var xfm = new ActiveXObject("XFileMngr.FileManager.1");
xfm.download(
'http://somedomain/somefile','localdev:\\folder\localfile',function(msg){alert(msg);});
}
/* C++ code */
// main routine
STDMETHODIMP CFileManager::download(BSTR url, BSTR local, VARIANT scriptCallback)
{
CString csURL(url);
CString csLocal(local);
CAsyncDownload download;
download.Download(this, csURL, csLocal, scriptCallback);
return S_OK;
}
// parts of CAsyncDownload.h
typedef struct tagThreadData {
CAsyncDownload* pThis;
} THREAD_DATA, *LPTHREAD_DATA;
class CAsyncDownload :
public IBindStatusCallback
{
private:
LPUNKNOWN pcaller;
CString csRemoteFile;
CString csLocalFile;
CComPtr<IDispatch> spCallback;
public:
void onDone(HRESULT hr);
HRESULT Download(LPUNKNOWN caller, CString& csRemote, CString& csLocal, VARIANT callback);
static DWORD __stdcall ThreadProc(void* param);
};
// parts of CAsyncDownload.cpp
void CAsyncDownload::onDone(HRESULT hr) {
if(spCallback) {
TRACE(TEXT("invoke callback function\n"));
CComVariant vParams[1];
vParams[0] = "callback is working!";
DISPPARAMS params = { vParams, NULL, 1, 0 };
HRESULT hr = spCallback->Invoke(0,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD,
¶ms, NULL, NULL, NULL);
if(FAILED(hr)) {
CString csBuffer;
csBuffer.Format(TEXT("invoke failed, result value: %d \n"),hr);
TRACE(csBuffer);
}else {
TRACE(TEXT("invoke was successful\n"));
}
}
}
HRESULT CAsyncDownload::Download(LPUNKNOWN caller, CString& csRemote, CString& csLocal, VARIANT callback) {
CoInitializeEx(NULL, COINIT_MULTITHREADED);
csRemoteFile = csRemote;
csLocalFile = csLocal;
pcaller = caller;
switch(callback.vt){
case VT_DISPATCH:
case VT_VARIANT:{
spCallback = callback.pdispVal;
}
break;
default:{
spCallback = NULL;
}
}
LPTHREAD_DATA pData = new THREAD_DATA;
pData->pThis = this;
// create monitor thread M
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, (void*)(pData), 0, NULL);
if(!hThread) {
delete pData;
return HRESULT_FROM_WIN32(GetLastError());
}
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
CoUninitialize();
return S_OK;
}
DWORD __stdcall CAsyncDownload::ThreadProc(void* param) {
LPTHREAD_DATA pData = (LPTHREAD_DATA)param;
// here, we will create http download thread D
// when download job is finish, call onDone method;
pData->pThis->onDone(S_OK);
delete pData;
return 0;
}
**************** CODE FINISH **************** OK, above is parts of my source code, if I call onDone method in sub-thread, I will get OLE ERROR(-2147418113 (8000FFFF) Catastrophic failure.). Did I miss something? please help me to figure it out.
© Stack Overflow or respective owner