1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/win32ole/win32ole.c: use typed data for WIN32OLE.

* ext/win32ole/win32ole.h: ditto.
* ext/win32ole/win32ole_event.c: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48665 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
suke 2014-12-01 11:03:13 +00:00
parent 15cfc9553a
commit 7f411d6ed4
4 changed files with 73 additions and 52 deletions

View file

@ -1,3 +1,9 @@
Mon Dec 1 20:01:12 2014 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c: use typed data for WIN32OLE.
* ext/win32ole/win32ole.h: ditto.
* ext/win32ole/win32ole_event.c: ditto.
Mon Dec 1 17:20:42 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Dec 1 17:20:42 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* win32/Makefile.sub (verconf.h): so depends on verconf.mk, which * win32/Makefile.sub (verconf.h): so depends on verconf.mk, which

View file

@ -97,7 +97,8 @@ static void load_conv_function51932(void);
static UINT ole_init_cp(void); static UINT ole_init_cp(void);
static void ole_freeexceptinfo(EXCEPINFO *pExInfo); static void ole_freeexceptinfo(EXCEPINFO *pExInfo);
static VALUE ole_excepinfo2msg(EXCEPINFO *pExInfo); static VALUE ole_excepinfo2msg(EXCEPINFO *pExInfo);
static void ole_free(struct oledata *pole); static void ole_free(void *ptr);
static size_t ole_size(const void *ptr);
static LPWSTR ole_mb2wc(char *pm, int len); static LPWSTR ole_mb2wc(char *pm, int len);
static VALUE ole_ary_m_entry(VALUE val, LONG *pid); static VALUE ole_ary_m_entry(VALUE val, LONG *pid);
static VALUE is_all_index_under(LONG *pid, long *pub, long dim); static VALUE is_all_index_under(LONG *pid, long *pub, long dim);
@ -168,6 +169,12 @@ static VALUE fole_activex_initialize(VALUE self);
static void init_enc2cp(void); static void init_enc2cp(void);
static void free_enc2cp(void); static void free_enc2cp(void);
static const rb_data_type_t ole_datatype = {
"win32ole",
{NULL, ole_free, ole_size,},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};
static HRESULT (STDMETHODCALLTYPE mf_QueryInterface)( static HRESULT (STDMETHODCALLTYPE mf_QueryInterface)(
IMessageFilter __RPC_FAR * This, IMessageFilter __RPC_FAR * This,
/* [in] */ REFIID riid, /* [in] */ REFIID riid,
@ -813,12 +820,26 @@ ole_initialize(void)
} }
static void static void
ole_free(struct oledata *pole) ole_free(void *ptr)
{ {
struct oledata *pole = ptr;
OLE_FREE(pole->pDispatch); OLE_FREE(pole->pDispatch);
free(pole); free(pole);
} }
static size_t ole_size(const void *ptr)
{
return ptr ? sizeof(struct oledata) : 0;
}
struct oledata *
oledata_get_struct(VALUE ole)
{
struct oledata *pole;
TypedData_Get_Struct(ole, struct oledata, &ole_datatype, pole);
return pole;
}
LPWSTR LPWSTR
ole_vstr2wc(VALUE vstr) ole_vstr2wc(VALUE vstr)
{ {
@ -1216,9 +1237,9 @@ ole_val_ary2variant_ary(VALUE val, VARIANT *var, VARTYPE vt)
void void
ole_val2variant(VALUE val, VARIANT *var) ole_val2variant(VALUE val, VARIANT *var)
{ {
struct oledata *pole; struct oledata *pole = NULL;
if(rb_obj_is_kind_of(val, cWIN32OLE)) { if(rb_obj_is_kind_of(val, cWIN32OLE)) {
Data_Get_Struct(val, struct oledata, pole); pole = oledata_get_struct(val);
OLE_ADDREF(pole->pDispatch); OLE_ADDREF(pole->pDispatch);
V_VT(var) = VT_DISPATCH; V_VT(var) = VT_DISPATCH;
V_DISPATCH(var) = pole->pDispatch; V_DISPATCH(var) = pole->pDispatch;
@ -1310,8 +1331,8 @@ default_inspect(VALUE self, const char *class_name)
static VALUE static VALUE
ole_set_member(VALUE self, IDispatch *dispatch) ole_set_member(VALUE self, IDispatch *dispatch)
{ {
struct oledata *pole; struct oledata *pole = NULL;
Data_Get_Struct(self, struct oledata, pole); pole = oledata_get_struct(self);
if (pole->pDispatch) { if (pole->pDispatch) {
OLE_RELEASE(pole->pDispatch); OLE_RELEASE(pole->pDispatch);
pole->pDispatch = NULL; pole->pDispatch = NULL;
@ -1327,7 +1348,7 @@ fole_s_allocate(VALUE klass)
struct oledata *pole; struct oledata *pole;
VALUE obj; VALUE obj;
ole_initialize(); ole_initialize();
obj = Data_Make_Struct(klass,struct oledata,0,ole_free,pole); obj = TypedData_Make_Struct(klass, struct oledata, &ole_datatype, pole);
pole->pDispatch = NULL; pole->pDispatch = NULL;
return obj; return obj;
} }
@ -2007,7 +2028,7 @@ fole_s_const_load(int argc, VALUE *argv, VALUE self)
{ {
VALUE ole; VALUE ole;
VALUE klass; VALUE klass;
struct oledata *pole; struct oledata *pole = NULL;
ITypeInfo *pTypeInfo; ITypeInfo *pTypeInfo;
ITypeLib *pTypeLib; ITypeLib *pTypeLib;
unsigned int index; unsigned int index;
@ -2023,7 +2044,7 @@ fole_s_const_load(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eTypeError, "2nd parameter must be Class or Module"); rb_raise(rb_eTypeError, "2nd parameter must be Class or Module");
} }
if (rb_obj_is_kind_of(ole, cWIN32OLE)) { if (rb_obj_is_kind_of(ole, cWIN32OLE)) {
OLEData_Get_Struct(ole, pole); pole = oledata_get_struct(ole);
hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch, hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch,
0, lcid, &pTypeInfo); 0, lcid, &pTypeInfo);
if(FAILED(hr)) { if(FAILED(hr)) {
@ -2089,8 +2110,8 @@ reference_count(struct oledata * pole)
static VALUE static VALUE
fole_s_reference_count(VALUE self, VALUE obj) fole_s_reference_count(VALUE self, VALUE obj)
{ {
struct oledata * pole; struct oledata * pole = NULL;
OLEData_Get_Struct(obj, pole); pole = oledata_get_struct(obj);
return INT2NUM(reference_count(pole)); return INT2NUM(reference_count(pole));
} }
@ -2107,8 +2128,8 @@ static VALUE
fole_s_free(VALUE self, VALUE obj) fole_s_free(VALUE self, VALUE obj)
{ {
ULONG n = 0; ULONG n = 0;
struct oledata * pole; struct oledata * pole = NULL;
OLEData_Get_Struct(obj, pole); pole = oledata_get_struct(obj);
if(pole->pDispatch) { if(pole->pDispatch) {
if (reference_count(pole) > 0) { if (reference_count(pole) > 0) {
n = OLE_RELEASE(pole->pDispatch); n = OLE_RELEASE(pole->pDispatch);
@ -2516,7 +2537,7 @@ static VALUE
ole_invoke(int argc, VALUE *argv, VALUE self, USHORT wFlags, BOOL is_bracket) ole_invoke(int argc, VALUE *argv, VALUE self, USHORT wFlags, BOOL is_bracket)
{ {
LCID lcid = cWIN32OLE_lcid; LCID lcid = cWIN32OLE_lcid;
struct oledata *pole; struct oledata *pole = NULL;
HRESULT hr; HRESULT hr;
VALUE cmd; VALUE cmd;
VALUE paramS; VALUE paramS;
@ -2552,7 +2573,7 @@ ole_invoke(int argc, VALUE *argv, VALUE self, USHORT wFlags, BOOL is_bracket)
if (RB_TYPE_P(cmd, T_SYMBOL)) { if (RB_TYPE_P(cmd, T_SYMBOL)) {
cmd = rb_sym_to_s(cmd); cmd = rb_sym_to_s(cmd);
} }
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
if(!pole->pDispatch) { if(!pole->pDispatch) {
rb_raise(rb_eRuntimeError, "failed to get dispatch interface"); rb_raise(rb_eRuntimeError, "failed to get dispatch interface");
} }
@ -2773,7 +2794,7 @@ static VALUE
ole_invoke2(VALUE self, VALUE dispid, VALUE args, VALUE types, USHORT dispkind) ole_invoke2(VALUE self, VALUE dispid, VALUE args, VALUE types, USHORT dispkind)
{ {
HRESULT hr; HRESULT hr;
struct oledata *pole; struct oledata *pole = NULL;
unsigned int argErr = 0; unsigned int argErr = 0;
EXCEPINFO excepinfo; EXCEPINFO excepinfo;
VARIANT result; VARIANT result;
@ -2791,7 +2812,7 @@ ole_invoke2(VALUE self, VALUE dispid, VALUE args, VALUE types, USHORT dispkind)
memset(&excepinfo, 0, sizeof(EXCEPINFO)); memset(&excepinfo, 0, sizeof(EXCEPINFO));
memset(&dispParams, 0, sizeof(DISPPARAMS)); memset(&dispParams, 0, sizeof(DISPPARAMS));
VariantInit(&result); VariantInit(&result);
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
dispParams.cArgs = RARRAY_LEN(args); dispParams.cArgs = RARRAY_LEN(args);
dispParams.rgvarg = ALLOCA_N(VARIANTARG, dispParams.cArgs); dispParams.rgvarg = ALLOCA_N(VARIANTARG, dispParams.cArgs);
@ -3081,7 +3102,7 @@ fole_getproperty_with_bracket(int argc, VALUE *argv, VALUE self)
static VALUE static VALUE
ole_propertyput(VALUE self, VALUE property, VALUE value) ole_propertyput(VALUE self, VALUE property, VALUE value)
{ {
struct oledata *pole; struct oledata *pole = NULL;
unsigned argErr; unsigned argErr;
unsigned int index; unsigned int index;
HRESULT hr; HRESULT hr;
@ -3103,7 +3124,7 @@ ole_propertyput(VALUE self, VALUE property, VALUE value)
VariantInit(&propertyValue[1]); VariantInit(&propertyValue[1]);
memset(&excepinfo, 0, sizeof(excepinfo)); memset(&excepinfo, 0, sizeof(excepinfo));
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
/* get ID from property name */ /* get ID from property name */
pBuf[0] = ole_vstr2wc(property); pBuf[0] = ole_vstr2wc(property);
@ -3147,8 +3168,8 @@ ole_propertyput(VALUE self, VALUE property, VALUE value)
static VALUE static VALUE
fole_free(VALUE self) fole_free(VALUE self)
{ {
struct oledata *pole; struct oledata *pole = NULL;
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
OLE_FREE(pole->pDispatch); OLE_FREE(pole->pDispatch);
pole->pDispatch = NULL; pole->pDispatch = NULL;
return Qnil; return Qnil;
@ -3197,7 +3218,7 @@ fole_each(VALUE self)
{ {
LCID lcid = cWIN32OLE_lcid; LCID lcid = cWIN32OLE_lcid;
struct oledata *pole; struct oledata *pole = NULL;
unsigned int argErr; unsigned int argErr;
EXCEPINFO excepinfo; EXCEPINFO excepinfo;
@ -3216,7 +3237,7 @@ fole_each(VALUE self)
dispParams.cArgs = 0; dispParams.cArgs = 0;
memset(&excepinfo, 0, sizeof(excepinfo)); memset(&excepinfo, 0, sizeof(excepinfo));
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
hr = pole->pDispatch->lpVtbl->Invoke(pole->pDispatch, DISPID_NEWENUM, hr = pole->pDispatch->lpVtbl->Invoke(pole->pDispatch, DISPID_NEWENUM,
&IID_NULL, lcid, &IID_NULL, lcid,
DISPATCH_METHOD | DISPATCH_PROPERTYGET, DISPATCH_METHOD | DISPATCH_PROPERTYGET,
@ -3332,9 +3353,9 @@ ole_methods(VALUE self, int mask)
ITypeInfo *pTypeInfo; ITypeInfo *pTypeInfo;
HRESULT hr; HRESULT hr;
VALUE methods; VALUE methods;
struct oledata *pole; struct oledata *pole = NULL;
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
methods = rb_ary_new(); methods = rb_ary_new();
hr = typeinfo_from_ole(pole, &pTypeInfo); hr = typeinfo_from_ole(pole, &pTypeInfo);
@ -3425,11 +3446,11 @@ fole_type(VALUE self)
{ {
ITypeInfo *pTypeInfo; ITypeInfo *pTypeInfo;
HRESULT hr; HRESULT hr;
struct oledata *pole; struct oledata *pole = NULL;
LCID lcid = cWIN32OLE_lcid; LCID lcid = cWIN32OLE_lcid;
VALUE type = Qnil; VALUE type = Qnil;
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
hr = pole->pDispatch->lpVtbl->GetTypeInfo( pole->pDispatch, 0, lcid, &pTypeInfo ); hr = pole->pDispatch->lpVtbl->GetTypeInfo( pole->pDispatch, 0, lcid, &pTypeInfo );
if(FAILED(hr)) { if(FAILED(hr)) {
@ -3457,13 +3478,13 @@ fole_type(VALUE self)
static VALUE static VALUE
fole_typelib(VALUE self) fole_typelib(VALUE self)
{ {
struct oledata *pole; struct oledata *pole = NULL;
HRESULT hr; HRESULT hr;
ITypeInfo *pTypeInfo; ITypeInfo *pTypeInfo;
LCID lcid = cWIN32OLE_lcid; LCID lcid = cWIN32OLE_lcid;
VALUE vtlib = Qnil; VALUE vtlib = Qnil;
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch, hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch,
0, lcid, &pTypeInfo); 0, lcid, &pTypeInfo);
if(FAILED(hr)) { if(FAILED(hr)) {
@ -3493,7 +3514,7 @@ fole_query_interface(VALUE self, VALUE str_iid)
HRESULT hr; HRESULT hr;
OLECHAR *pBuf; OLECHAR *pBuf;
IID iid; IID iid;
struct oledata *pole; struct oledata *pole = NULL;
IDispatch *pDispatch; IDispatch *pDispatch;
void *p; void *p;
@ -3506,7 +3527,7 @@ fole_query_interface(VALUE self, VALUE str_iid)
StringValuePtr(str_iid)); StringValuePtr(str_iid));
} }
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
if(!pole->pDispatch) { if(!pole->pDispatch) {
rb_raise(rb_eRuntimeError, "failed to get dispatch interface"); rb_raise(rb_eRuntimeError, "failed to get dispatch interface");
} }
@ -3535,7 +3556,7 @@ fole_query_interface(VALUE self, VALUE str_iid)
static VALUE static VALUE
fole_respond_to(VALUE self, VALUE method) fole_respond_to(VALUE self, VALUE method)
{ {
struct oledata *pole; struct oledata *pole = NULL;
BSTR wcmdname; BSTR wcmdname;
DISPID DispID; DISPID DispID;
HRESULT hr; HRESULT hr;
@ -3545,7 +3566,7 @@ fole_respond_to(VALUE self, VALUE method)
if (RB_TYPE_P(method, T_SYMBOL)) { if (RB_TYPE_P(method, T_SYMBOL)) {
method = rb_sym_to_s(method); method = rb_sym_to_s(method);
} }
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
wcmdname = ole_vstr2wc(method); wcmdname = ole_vstr2wc(method);
hr = pole->pDispatch->lpVtbl->GetIDsOfNames( pole->pDispatch, &IID_NULL, hr = pole->pDispatch->lpVtbl->GetIDsOfNames( pole->pDispatch, &IID_NULL,
&wcmdname, 1, cWIN32OLE_lcid, &DispID); &wcmdname, 1, cWIN32OLE_lcid, &DispID);
@ -3748,11 +3769,11 @@ fole_method_help(VALUE self, VALUE cmdname)
{ {
ITypeInfo *pTypeInfo; ITypeInfo *pTypeInfo;
HRESULT hr; HRESULT hr;
struct oledata *pole; struct oledata *pole = NULL;
VALUE obj; VALUE obj;
SafeStringValue(cmdname); SafeStringValue(cmdname);
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
hr = typeinfo_from_ole(pole, &pTypeInfo); hr = typeinfo_from_ole(pole, &pTypeInfo);
if(FAILED(hr)) if(FAILED(hr))
ole_raise(hr, rb_eRuntimeError, "failed to get ITypeInfo"); ole_raise(hr, rb_eRuntimeError, "failed to get ITypeInfo");
@ -3789,13 +3810,13 @@ fole_method_help(VALUE self, VALUE cmdname)
static VALUE static VALUE
fole_activex_initialize(VALUE self) fole_activex_initialize(VALUE self)
{ {
struct oledata *pole; struct oledata *pole = NULL;
IPersistMemory *pPersistMemory; IPersistMemory *pPersistMemory;
void *p; void *p;
HRESULT hr = S_OK; HRESULT hr = S_OK;
OLEData_Get_Struct(self, pole); pole = oledata_get_struct(self);
hr = pole->pDispatch->lpVtbl->QueryInterface(pole->pDispatch, &IID_IPersistMemory, &p); hr = pole->pDispatch->lpVtbl->QueryInterface(pole->pDispatch, &IID_IPersistMemory, &p);
pPersistMemory = p; pPersistMemory = p;
@ -3819,10 +3840,10 @@ typelib_from_val(VALUE obj, ITypeLib **pTypeLib)
{ {
LCID lcid = cWIN32OLE_lcid; LCID lcid = cWIN32OLE_lcid;
HRESULT hr; HRESULT hr;
struct oledata *pole; struct oledata *pole = NULL;
unsigned int index; unsigned int index;
ITypeInfo *pTypeInfo; ITypeInfo *pTypeInfo;
OLEData_Get_Struct(obj, pole); pole = oledata_get_struct(obj);
hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch, hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch,
0, lcid, &pTypeInfo); 0, lcid, &pTypeInfo);
if (FAILED(hr)) { if (FAILED(hr)) {

View file

@ -112,16 +112,10 @@ struct oledata {
IDispatch *pDispatch; IDispatch *pDispatch;
}; };
#define OLEData_Get_Struct(obj, pole) {\
Data_Get_Struct(obj, struct oledata, pole);\
if(!pole->pDispatch) {\
rb_raise(rb_eRuntimeError, "failed to get Dispatch Interface");\
}\
}
VALUE cWIN32OLE; VALUE cWIN32OLE;
LCID cWIN32OLE_lcid; LCID cWIN32OLE_lcid;
struct oledata *oledata_get_struct(VALUE obj);
LPWSTR ole_vstr2wc(VALUE vstr); LPWSTR ole_vstr2wc(VALUE vstr);
LONG reg_open_key(HKEY hkey, const char *name, HKEY *phkey); LONG reg_open_key(HKEY hkey, const char *name, HKEY *phkey);
LONG reg_open_vkey(HKEY hkey, VALUE key, HKEY *phkey); LONG reg_open_vkey(HKEY hkey, VALUE key, HKEY *phkey);

View file

@ -485,7 +485,7 @@ find_iid(VALUE ole, char *pitf, IID *piid, ITypeInfo **ppTypeInfo)
ITypeInfo *pImplTypeInfo; ITypeInfo *pImplTypeInfo;
TYPEATTR *pImplTypeAttr; TYPEATTR *pImplTypeAttr;
struct oledata *pole; struct oledata *pole = NULL;
unsigned int index; unsigned int index;
unsigned int count; unsigned int count;
int type; int type;
@ -495,7 +495,7 @@ find_iid(VALUE ole, char *pitf, IID *piid, ITypeInfo **ppTypeInfo)
BOOL is_found = FALSE; BOOL is_found = FALSE;
LCID lcid = cWIN32OLE_lcid; LCID lcid = cWIN32OLE_lcid;
OLEData_Get_Struct(ole, pole); pole = oledata_get_struct(ole);
pDispatch = pole->pDispatch; pDispatch = pole->pDispatch;
@ -709,9 +709,9 @@ find_default_source(VALUE ole, IID *piid, ITypeInfo **ppTypeInfo)
TYPEATTR *pTypeAttr; TYPEATTR *pTypeAttr;
TYPEATTR *pTypeAttr2 = NULL; TYPEATTR *pTypeAttr2 = NULL;
struct oledata *pole; struct oledata *pole = NULL;
OLEData_Get_Struct(ole, pole); pole = oledata_get_struct(ole);
pDispatch = pole->pDispatch; pDispatch = pole->pDispatch;
hr = pDispatch->lpVtbl->QueryInterface(pDispatch, hr = pDispatch->lpVtbl->QueryInterface(pDispatch,
&IID_IProvideClassInfo2, &IID_IProvideClassInfo2,
@ -896,7 +896,7 @@ ev_advise(int argc, VALUE *argv, VALUE self)
{ {
VALUE ole, itf; VALUE ole, itf;
struct oledata *pole; struct oledata *pole = NULL;
char *pitf; char *pitf;
HRESULT hr; HRESULT hr;
IID iid; IID iid;
@ -930,7 +930,7 @@ ev_advise(int argc, VALUE *argv, VALUE self)
ole_raise(hr, rb_eRuntimeError, "interface not found"); ole_raise(hr, rb_eRuntimeError, "interface not found");
} }
OLEData_Get_Struct(ole, pole); pole = oledata_get_struct(ole);
pDispatch = pole->pDispatch; pDispatch = pole->pDispatch;
hr = pDispatch->lpVtbl->QueryInterface(pDispatch, hr = pDispatch->lpVtbl->QueryInterface(pDispatch,
&IID_IConnectionPointContainer, &IID_IConnectionPointContainer,