| In: |
win32ole/win32ole.c
|
| Parent: | Object |
WIN32OLE.connect( ole ) —> aWIN32OLE
Returns running OLE Automation object or WIN32OLE object from moniker. 1st argument should be OLE program id or class id or moniker.
/*
* WIN32OLE.connect( ole ) --> aWIN32OLE
* ----
* Returns running OLE Automation object or WIN32OLE object from moniker.
* 1st argument should be OLE program id or class id or moniker.
*/
static VALUE
fole_s_connect(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE svr_name;
VALUE others;
HRESULT hr;
CLSID clsid;
OLECHAR *pBuf;
IDispatch *pDispatch;
IUnknown *pUnknown;
rb_secure(4);
/* initialize to use OLE */
ole_initialize();
rb_scan_args(argc, argv, "1*", &svr_name, &others);
if (ruby_safe_level > 0 && OBJ_TAINTED(svr_name)) {
rb_raise(rb_eSecurityError, "Insecure Object Connection - %s",
StringValuePtr(svr_name));
}
/* get CLSID from OLE server name */
pBuf = ole_mb2wc(StringValuePtr(svr_name), -1);
hr = CLSIDFromProgID(pBuf, &clsid);
if(FAILED(hr)) {
hr = CLSIDFromString(pBuf, &clsid);
}
SysFreeString(pBuf);
if(FAILED(hr)) {
return ole_bind_obj(svr_name, argc, argv, self);
}
hr = GetActiveObject(&clsid, 0, &pUnknown);
if (FAILED(hr)) {
ole_raise(hr, eWIN32OLE_RUNTIME_ERROR,
"Not Running OLE server : `%s'", StringValuePtr(svr_name));
}
hr = pUnknown->lpVtbl->QueryInterface(pUnknown, &IID_IDispatch,
(void **)&pDispatch);
if(FAILED(hr)) {
OLE_RELEASE(pUnknown);
ole_raise(hr, eWIN32OLE_RUNTIME_ERROR,
"failed to create WIN32OLE server : `%s'",
StringValuePtr(svr_name));
}
OLE_RELEASE(pUnknown);
return create_win32ole_object(self, pDispatch, argc, argv);
}
WIN32OLE.const_load( ole, mod = WIN32OLE)
Defines the constants of OLE Automation server as mod’s constants. If 2nd argument is omitted, the default is WIN32OLE.
/*
* WIN32OLE.const_load( ole, mod = WIN32OLE)
* ----
* Defines the constants of OLE Automation server as mod's constants.
* If 2nd argument is omitted, the default is WIN32OLE.
*/
static VALUE
fole_s_const_load(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE ole;
VALUE klass;
struct oledata *pole;
ITypeInfo *pTypeInfo;
ITypeLib *pTypeLib;
unsigned int index;
HRESULT hr;
OLECHAR *pBuf;
VALUE file;
LCID lcid = LOCALE_SYSTEM_DEFAULT;
rb_secure(4);
rb_scan_args(argc, argv, "11", &ole, &klass);
if (TYPE(klass) != T_CLASS &&
TYPE(klass) != T_MODULE &&
TYPE(klass) != T_NIL) {
rb_raise(rb_eTypeError, "2nd paramator must be Class or Module.");
}
if (rb_obj_is_kind_of(ole, cWIN32OLE)) {
OLEData_Get_Struct(ole, pole);
hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch,
0, lcid, &pTypeInfo);
if(FAILED(hr)) {
ole_raise(hr, rb_eRuntimeError, "failed to GetTypeInfo");
}
hr = pTypeInfo->lpVtbl->GetContainingTypeLib(pTypeInfo, &pTypeLib, &index);
if(FAILED(hr)) {
OLE_RELEASE(pTypeInfo);
ole_raise(hr, rb_eRuntimeError, "failed to GetContainingTypeLib");
}
OLE_RELEASE(pTypeInfo);
if(TYPE(klass) != T_NIL) {
ole_const_load(pTypeLib, klass, self);
}
else {
ole_const_load(pTypeLib, cWIN32OLE, self);
}
OLE_RELEASE(pTypeLib);
}
else if(TYPE(ole) == T_STRING) {
file = typelib_file(ole);
if (file == Qnil) {
file = ole;
}
pBuf = ole_mb2wc(StringValuePtr(file), -1);
hr = LoadTypeLibEx(pBuf, REGKIND_NONE, &pTypeLib);
SysFreeString(pBuf);
if (FAILED(hr))
ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "failed to LoadTypeLibEx");
if(TYPE(klass) != T_NIL) {
ole_const_load(pTypeLib, klass, self);
}
else {
ole_const_load(pTypeLib, cWIN32OLE, self);
}
OLE_RELEASE(pTypeLib);
}
else {
rb_raise(rb_eTypeError, "1st paramator must be WIN32OLE instance");
}
return Qnil;
}
WIN32OLE.ole_free(aWIN32OLE) —> number
Invokes Release method of Dispatch interface of WIN32OLE object. You should not use this method because this method exists only for debugging WIN32OLE. The return value is reference counter of OLE object.
/*
* WIN32OLE.ole_free(aWIN32OLE) --> number
* ----
* Invokes Release method of Dispatch interface of WIN32OLE object.
* You should not use this method because this method
* exists only for debugging WIN32OLE.
* The return value is reference counter of OLE object.
*/
static VALUE
fole_s_free(self, obj)
VALUE self;
VALUE obj;
{
ULONG n = 0;
struct oledata * pole;
OLEData_Get_Struct(obj, pole);
if(pole->pDispatch) {
if (reference_count(pole) > 0) {
n = OLE_RELEASE(pole->pDispatch);
}
}
return INT2NUM(n);
}
WIN32OLE.ole_reference_count(aWIN32OLE) —> number
Returns reference counter of Dispatch interface of WIN32OLE object. You should not use this method because this method exists only for debugging WIN32OLE.
/*
* WIN32OLE.ole_reference_count(aWIN32OLE) --> number
* ----
* Returns reference counter of Dispatch interface of WIN32OLE object.
* You should not use this method because this method
* exists only for debugging WIN32OLE.
*/
static VALUE
fole_s_reference_count(self, obj)
VALUE self;
VALUE obj;
{
struct oledata * pole;
OLEData_Get_Struct(obj, pole);
return INT2NUM(reference_count(pole));
}
WIN32OLE.ole_show_help(obj [,helpcontext])
Displays helpfile. The 1st argument specifies WIN32OLE_TYPE object or WIN32OLE_METHOD object or helpfile.
/*
* WIN32OLE.ole_show_help(obj [,helpcontext])
* ----
* Displays helpfile. The 1st argument specifies WIN32OLE_TYPE
* object or WIN32OLE_METHOD object or helpfile.
*/
static VALUE
fole_s_show_help(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE target;
VALUE helpcontext;
VALUE helpfile;
VALUE name;
HWND hwnd;
rb_scan_args(argc, argv, "11", &target, &helpcontext);
if (rb_obj_is_kind_of(target, cWIN32OLE_TYPE) ||
rb_obj_is_kind_of(target, cWIN32OLE_METHOD)) {
helpfile = rb_funcall(target, rb_intern("helpfile"), 0);
if(strlen(StringValuePtr(helpfile)) == 0) {
name = rb_ivar_get(target, rb_intern("name"));
rb_raise(rb_eRuntimeError, "no helpfile of `%s'",
StringValuePtr(name));
}
helpcontext = rb_funcall(target, rb_intern("helpcontext"), 0);
} else {
helpfile = target;
}
if (TYPE(helpfile) != T_STRING) {
rb_raise(rb_eTypeError, "1st parameter must be (String|WIN32OLE_TYPE|WIN32OLE_METHOD).");
}
hwnd = ole_show_help(helpfile, helpcontext);
if(hwnd == 0) {
rb_raise(rb_eRuntimeError, "failed to open help file:%s",
StringValuePtr(helpfile));
}
return Qnil;
}
WIN32OLE[‘property’]=val
WIN32OLE.setproperty(‘property’, [arg1, arg2,] val)
Sets property of OLE object. When you want to set property with argument, you can use setproperty method.
/*
* WIN32OLE['property']=val
*
* WIN32OLE.setproperty('property', [arg1, arg2,] val)
* -----
* Sets property of OLE object.
* When you want to set property with argument, you can use setproperty method.
*/
static VALUE
fole_setproperty(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
return ole_invoke(argc, argv, self, DISPATCH_PROPERTYPUT);
}
WIN32OLE#_getproperty(dispid, args, types)
Runs the early binding method to get property. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.
/*
* WIN32OLE#_getproperty(dispid, args, types)
* ----
* Runs the early binding method to get property.
* The 1st argument specifies dispatch ID,
* the 2nd argument specifies the array of arguments,
* the 3rd argument specifies the array of the type of arguments.
*/
static VALUE
fole_getproperty2(self, dispid, args, types)
VALUE self;
VALUE dispid;
VALUE args;
VALUE types;
{
return ole_invoke2(self, dispid, args, types, DISPATCH_PROPERTYGET);
}
WIN32OLE#_invoke(dispid, args, types)
Runs the early binding method. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.
/*
* WIN32OLE#_invoke(dispid, args, types)
* ----
* Runs the early binding method.
* The 1st argument specifies dispatch ID,
* the 2nd argument specifies the array of arguments,
* the 3rd argument specifies the array of the type of arguments.
*/
static VALUE
fole_invoke2(self, dispid, args, types)
VALUE self;
VALUE dispid;
VALUE args;
VALUE types;
{
return ole_invoke2(self, dispid, args, types, DISPATCH_METHOD);
}
WIN32OLE#_setproperty(dispid, args, types)
Runs the early binding method to set property. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.
/*
* WIN32OLE#_setproperty(dispid, args, types)
* ----
* Runs the early binding method to set property.
* The 1st argument specifies dispatch ID,
* the 2nd argument specifies the array of arguments,
* the 3rd argument specifies the array of the type of arguments.
*/
static VALUE
fole_setproperty2(self, dispid, args, types)
VALUE self;
VALUE dispid;
VALUE args;
VALUE types;
{
return ole_invoke2(self, dispid, args, types, DISPATCH_PROPERTYPUT);
}
WIN32OLE#each {|i|…}
Iterates over each item of OLE collection which has IEnumVARIANT interface.
/*
* WIN32OLE#each {|i|...}
* -----
* Iterates over each item of OLE collection which has IEnumVARIANT interface.
*/
static VALUE
fole_each(self)
VALUE self;
{
LCID lcid = LOCALE_SYSTEM_DEFAULT;
struct oledata *pole;
unsigned int argErr;
EXCEPINFO excepinfo;
DISPPARAMS dispParams;
VARIANT result;
HRESULT hr;
IEnumVARIANT *pEnum = NULL;
VariantInit(&result);
dispParams.rgvarg = NULL;
dispParams.rgdispidNamedArgs = NULL;
dispParams.cNamedArgs = 0;
dispParams.cArgs = 0;
memset(&excepinfo, 0, sizeof(excepinfo));
OLEData_Get_Struct(self, pole);
hr = pole->pDispatch->lpVtbl->Invoke(pole->pDispatch, DISPID_NEWENUM,
&IID_NULL, lcid,
DISPATCH_METHOD | DISPATCH_PROPERTYGET,
&dispParams, &result,
&excepinfo, &argErr);
if (FAILED(hr)) {
VariantClear(&result);
ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "failed to get IEnum Interface");
}
if (V_VT(&result) == VT_UNKNOWN)
hr = V_UNKNOWN(&result)->lpVtbl->QueryInterface(V_UNKNOWN(&result),
&IID_IEnumVARIANT,
(void**)&pEnum);
else if (V_VT(&result) == VT_DISPATCH)
hr = V_DISPATCH(&result)->lpVtbl->QueryInterface(V_DISPATCH(&result),
&IID_IEnumVARIANT,
(void**)&pEnum);
if (FAILED(hr) || !pEnum) {
VariantClear(&result);
ole_raise(hr, rb_eRuntimeError, "failed to get IEnum Interface");
}
VariantClear(&result);
rb_ensure(ole_each_sub, (VALUE)pEnum, ole_ienum_free, (VALUE)pEnum);
return Qnil;
}
WIN32OLE#method_missing(id [,arg1, arg2, …])
Calls WIN32OLE#invoke method.
/*
* WIN32OLE#method_missing(id [,arg1, arg2, ...])
* ----
* Calls WIN32OLE#invoke method.
*/
static VALUE
fole_missing(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
ID id;
char* mname;
int n;
id = rb_to_id(argv[0]);
mname = rb_id2name(id);
if(!mname) {
rb_raise(rb_eRuntimeError, "Fail : Unknown method or property");
}
n = strlen(mname);
if(mname[n-1] == '=') {
argv[0] = rb_str_new(mname, n-1);
return ole_propertyput(self, argv[0], argv[1]);
}
else {
argv[0] = rb_str_new2(mname);
return ole_invoke(argc, argv, self, DISPATCH_METHOD|DISPATCH_PROPERTYGET);
}
}
Returns OLE func methods.
/*
* WIN32OLE#ole_func_methods
* ---
* Returns OLE func methods.
*/
static VALUE
fole_func_methods( argc, argv, self )
int argc;
VALUE *argv;
VALUE self;
{
return ole_methods( self, INVOKE_FUNC);
}
Returns get properties.
/*
* WIN32OLE#ole_get_methods
* ----
* Returns get properties.
*/
static VALUE
fole_get_methods( argc, argv, self )
int argc;
VALUE *argv;
VALUE self;
{
return ole_methods( self, INVOKE_PROPERTYGET);
}
WIN32OLE#ole_method_help(method)
Returns WIN32OLE_METHOD object corresponding with method specified by 1st argument.
/*
* WIN32OLE#ole_method_help(method)
* -----
* Returns WIN32OLE_METHOD object corresponding with method
* specified by 1st argument.
*/
static VALUE
fole_method_help( self, cmdname )
VALUE self;
VALUE cmdname;
{
ITypeInfo *pTypeInfo;
HRESULT hr;
struct oledata *pole;
VALUE method, obj;
LCID lcid = LOCALE_SYSTEM_DEFAULT;
Check_SafeStr(cmdname);
OLEData_Get_Struct(self, pole);
hr = typeinfo_from_ole(pole, &pTypeInfo);
if(FAILED(hr))
ole_raise(hr, rb_eRuntimeError, "failed to get ITypeInfo");
method = folemethod_s_allocate(cWIN32OLE_METHOD);
obj = olemethod_from_typeinfo(method, pTypeInfo, cmdname);
OLE_RELEASE(pTypeInfo);
if (obj == Qnil)
rb_raise(eWIN32OLE_RUNTIME_ERROR, "Not found %s",
StringValuePtr(cmdname));
return obj;
}
Returns OLE methods
/*
* WIN32OLE#ole_methods
* ----
* Returns OLE methods
*/
static VALUE
fole_methods( self )
VALUE self;
{
return ole_methods( self, INVOKE_FUNC | INVOKE_PROPERTYGET | INVOKE_PROPERTYPUT);
}
Returns WIN32OLE_TYPE object.
/*
* WIN32OLE#ole_obj_help
* ----
* Returns WIN32OLE_TYPE object.
*/
static VALUE
fole_obj_help( self )
VALUE self;
{
unsigned int index;
ITypeInfo *pTypeInfo;
ITypeLib *pTypeLib;
HRESULT hr;
struct oledata *pole;
BSTR bstr;
LCID lcid = LOCALE_SYSTEM_DEFAULT;
VALUE type = Qnil;
OLEData_Get_Struct(self, pole);
hr = pole->pDispatch->lpVtbl->GetTypeInfo( pole->pDispatch, 0, lcid, &pTypeInfo );
if(FAILED(hr)) {
ole_raise(hr, rb_eRuntimeError, "failed to GetTypeInfo");
}
hr = pTypeInfo->lpVtbl->GetContainingTypeLib( pTypeInfo, &pTypeLib, &index );
if(FAILED(hr)) {
OLE_RELEASE(pTypeInfo);
ole_raise(hr, rb_eRuntimeError, "failed to GetContainingTypeLib");
}
hr = pTypeLib->lpVtbl->GetDocumentation( pTypeLib, index,
&bstr, NULL, NULL, NULL);
if (SUCCEEDED(hr)) {
type = foletype_s_allocate(cWIN32OLE_TYPE);
oletype_set_member(type, pTypeInfo, WC2VSTR(bstr));
}
OLE_RELEASE(pTypeLib);
OLE_RELEASE(pTypeInfo);
return type;
}
Returns put properties.
/*
* WIN32OLE#ole_put_methods
* ----
* Returns put properties.
*/
static VALUE
fole_put_methods( argc, argv, self )
int argc;
VALUE *argv;
VALUE self;
{
return ole_methods( self, INVOKE_PROPERTYPUT);
}
WIN32OLE[‘property’]=val
WIN32OLE.setproperty(‘property’, [arg1, arg2,] val)
Sets property of OLE object. When you want to set property with argument, you can use setproperty method.
/*
* WIN32OLE['property']=val
*
* WIN32OLE.setproperty('property', [arg1, arg2,] val)
* -----
* Sets property of OLE object.
* When you want to set property with argument, you can use setproperty method.
*/
static VALUE
fole_setproperty(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
return ole_invoke(argc, argv, self, DISPATCH_PROPERTYPUT);
}