pátek 27. února 2009

... throw exception from ActiveX to Javascript

Developing a simple ActiveX I was not able to find clear information how to propagate exception from C++ code to Javascript. I'm using ATL and there are no exceptions when using ATL code.

The solutin is to implement ISupportErrorInfo and act accordingly when it's the only method is called (apropriate code is generated by Visual Studio). Then when an error is to be reported after a method of ActiveX control returns, use functions CreateErrorInfo/SetErrorInfo to fill the error information data and use E_FAIL return code of the method.

Now an exception is thrown in Javascript in the form of Error object with message property, which maps to what was set by ICreateErrorInfo.SetDescription.

That's all for today.

Example:
void setException(BSTR msg){
ICreateErrorInfo * cErrInf;
CreateErrorInfo(&cErrInf);
if (cErrInf==NULL) return;
cErrInf->SetDescription(msg);
cErrInf->SetGUID(component_IID);
IErrorInfo *err;
cErrInf->QueryInterface(&err);
SetErrorInfo(0,err);
}



...
setException(L"error mesaage");
return E_FAIL;
}