/*
* call-seq:
* WIN32OLE_EVENT.new(ole, event) #=> WIN32OLE_EVENT object.
*
* Returns OLE event object.
* The first argument specifies WIN32OLE object.
* The second argument specifies OLE event name.
* ie = WIN32OLE.new('InternetExplorer.Application')
* ev = WIN32OLE_EVENT.new(ie, 'DWebBrowserEvents')
*/
static VALUE
fev_initialize(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE ole, itf;
struct oledata *pole;
char *pitf;
HRESULT hr;
IID iid;
ITypeInfo *pTypeInfo;
IDispatch *pDispatch;
IConnectionPointContainer *pContainer;
IConnectionPoint *pConnectionPoint;
IEVENTSINKOBJ *pIEV;
DWORD dwCookie = 0;
struct oleeventdata *poleev;
rb_secure(4);
rb_scan_args(argc, argv, "11", &ole, &itf);
if (!rb_obj_is_kind_of(ole, cWIN32OLE)) {
rb_raise(rb_eTypeError, "1st parameter must be WIN32OLE object");
}
if(TYPE(itf) != T_NIL) {
if (ruby_safe_level > 0 && OBJ_TAINTED(itf)) {
rb_raise(rb_eSecurityError, "Insecure Event Creation - %s",
StringValuePtr(itf));
}
Check_SafeStr(itf);
pitf = StringValuePtr(itf);
hr = find_iid(ole, pitf, &iid, &pTypeInfo);
}
else {
hr = find_default_source(ole, &iid, &pTypeInfo);
}
if (FAILED(hr)) {
ole_raise(hr, rb_eRuntimeError, "interface not found");
}
OLEData_Get_Struct(ole, pole);
pDispatch = pole->pDispatch;
hr = pDispatch->lpVtbl->QueryInterface(pDispatch,
&IID_IConnectionPointContainer,
(void**)&pContainer);
if (FAILED(hr)) {
OLE_RELEASE(pTypeInfo);
ole_raise(hr, rb_eRuntimeError,
"failed to query IConnectionPointContainer");
}
hr = pContainer->lpVtbl->FindConnectionPoint(pContainer,
&iid,
&pConnectionPoint);
OLE_RELEASE(pContainer);
if (FAILED(hr)) {
OLE_RELEASE(pTypeInfo);
ole_raise(hr, rb_eRuntimeError, "failed to query IConnectionPoint");
}
pIEV = EVENTSINK_Constructor();
pIEV->m_iid = iid;
hr = pConnectionPoint->lpVtbl->Advise(pConnectionPoint,
(IUnknown*)pIEV,
&dwCookie);
if (FAILED(hr)) {
ole_raise(hr, rb_eRuntimeError, "Advise Error");
}
Data_Get_Struct(self, struct oleeventdata, poleev);
poleev->pEvent = pIEV;
poleev->pEvent->m_event_id
= NUM2INT(rb_funcall(ary_ole_event, rb_intern("length"), 0));
poleev->pEvent->pConnectionPoint = pConnectionPoint;
poleev->pEvent->pTypeInfo = pTypeInfo;
poleev->pEvent->m_dwCookie = dwCookie;
poleev->freed = 0;
poleev->pEvent->ptr_freed = &(poleev->freed);
rb_ary_push(ary_ole_event, self);
return self;
}