static VALUE
fole_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE svr_name;
VALUE host;
VALUE others;
VALUE opts;
HRESULT hr;
CLSID clsid;
OLECHAR *pBuf;
OLECHAR *key_buf;
IDispatch *pDispatch;
IClassFactory2 * pIClassFactory2;
void *p;
static ID keyword_ids[1];
VALUE kwargs[1];
rb_call_super(0, 0);
rb_scan_args(argc, argv, "11*:", &svr_name, &host, &others, &opts);
StringValue(svr_name);
if (rb_safe_level() > 0 && OBJ_TAINTED(svr_name)) {
rb_raise(rb_eSecurityError, "insecure object creation - `%s'",
StringValuePtr(svr_name));
}
if (!NIL_P(host)) {
StringValue(host);
if (rb_safe_level() > 0 && OBJ_TAINTED(host)) {
rb_raise(rb_eSecurityError, "insecure object creation - `%s'",
StringValuePtr(host));
}
return ole_create_dcom(self, svr_name, host, others);
}
/* get CLSID from OLE server name */
pBuf = ole_vstr2wc(svr_name);
hr = CLSIDFromProgID(pBuf, &clsid);
if(FAILED(hr)) {
hr = CLSIDFromString(pBuf, &clsid);
}
SysFreeString(pBuf);
if(FAILED(hr)) {
ole_raise(hr, eWIN32OLERuntimeError,
"unknown OLE server: `%s'",
StringValuePtr(svr_name));
}
if (!keyword_ids[0]) {
keyword_ids[0] = rb_intern_const("license");
}
rb_get_kwargs(opts, keyword_ids, 0, 1, kwargs);
if (kwargs[0] == Qundef) {
/* get IDispatch interface */
hr = CoCreateInstance(
&clsid,
NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
&IID_IDispatch,
&p
);
} else {
hr = CoGetClassObject(
&clsid,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
NULL,
&IID_IClassFactory2,
(LPVOID)&pIClassFactory2
);
if (hr == S_OK) {
key_buf = ole_vstr2wc(kwargs[0]);
hr = pIClassFactory2->lpVtbl->CreateInstanceLic(pIClassFactory2, NULL, NULL, &IID_IDispatch, key_buf, &p);
SysFreeString(key_buf);
OLE_RELEASE(pIClassFactory2);
}
}
pDispatch = p;
if(FAILED(hr)) {
ole_raise(hr, eWIN32OLERuntimeError,
"failed to create WIN32OLE object from `%s'",
StringValuePtr(svr_name));
}
ole_set_member(self, pDispatch);
return self;
}