Class: WIN32OLE_TYPELIB

Inherits:
Object
  • Object
show all
Defined in:
win32ole.c

Overview

WIN32OLE_TYPELIB objects represent OLE tyblib information.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(typelib[, version1, version2]) ⇒ WIN32OLE_TYPELIB object

Returns a new WIN32OLE_TYPELIB object.

The first argument typelib specifies OLE type library name or GUID or OLE library file. The second argument is major version or version of the type library. The third argument is minor version. The second argument and third argument are optional. If the first argument is type library name, then the second and third argument are ignored.

tlib1 = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
tlib2 = WIN32OLE_TYPELIB.new('{00020813-0000-0000-C000-000000000046}')
tlib3 = WIN32OLE_TYPELIB.new('{00020813-0000-0000-C000-000000000046}', 1.3)
tlib4 = WIN32OLE_TYPELIB.new('{00020813-0000-0000-C000-000000000046}', 1, 3)
tlib5 = WIN32OLE_TYPELIB.new("C:\\WINNT\\SYSTEM32\\SHELL32.DLL")
puts tlib1.name  # -> 'Microsoft Excel 9.0 Object Library'
puts tlib2.name  # -> 'Microsoft Excel 9.0 Object Library'
puts tlib3.name  # -> 'Microsoft Excel 9.0 Object Library'
puts tlib4.name  # -> 'Microsoft Excel 9.0 Object Library'
puts tlib5.name  # -> 'Microsoft Shell Controls And Automation'


5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
# File 'win32ole.c', line 5189

static VALUE
foletypelib_initialize(VALUE self, VALUE args)
{
    VALUE found = Qfalse;
    VALUE typelib = Qnil;
    int len = 0;
    OLECHAR * pbuf;
    ITypeLib *pTypeLib;
    HRESULT hr = S_OK;

    len = RARRAY_LEN(args);
    if (len < 1 || len > 3) {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", len);
    }

    typelib = rb_ary_entry(args, 0);

    SafeStringValue(typelib);

    found = oletypelib_search_registry(self, typelib);
    if (found == Qfalse) {
        found = oletypelib_search_registry2(self, args);
    }
    if (found == Qfalse) {
        pbuf = ole_vstr2wc(typelib);
        hr = LoadTypeLibEx(pbuf, REGKIND_NONE, &pTypeLib);
        SysFreeString(pbuf);
        if (SUCCEEDED(hr)) {
	    found = Qtrue;
	    oletypelib_set_member(self, pTypeLib);
        }
    }

    if (found == Qfalse) {
        rb_raise(eWIN32OLERuntimeError, "not found type library `%s`",
                 StringValuePtr(typelib));
    }
    return self;
}

Class Method Details

.typelibsObject

Returns the array of WIN32OLE_TYPELIB object.

tlibs = WIN32OLE_TYPELIB.typelibs


4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
# File 'win32ole.c', line 4968

static VALUE
foletypelib_s_typelibs(VALUE self)
{
    HKEY htypelib, hguid;
    DWORD i, j;
    LONG err;
    VALUE guid;
    VALUE version;
    VALUE name = Qnil;
    VALUE typelibs = rb_ary_new();
    VALUE typelib = Qnil;
    HRESULT hr;
    ITypeLib *pTypeLib;

    err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", &htypelib);
    if(err != ERROR_SUCCESS) {
        return typelibs;
    }
    for(i = 0; ; i++) {
        guid = reg_enum_key(htypelib, i);
        if (guid == Qnil)
            break;
        err = reg_open_vkey(htypelib, guid, &hguid);
        if (err != ERROR_SUCCESS)
            continue;
        for(j = 0; ; j++) {
            version = reg_enum_key(hguid, j);
            if (version == Qnil)
                break;
            if ( (name = reg_get_val2(hguid, StringValuePtr(version))) != Qnil ) {
		hr = oletypelib_from_guid(guid, version, &pTypeLib);
		if (SUCCEEDED(hr)) {
		    typelib = rb_funcall(cWIN32OLE_TYPELIB, rb_intern("allocate"), 0);
		    oletypelib_set_member(typelib, pTypeLib);
		    rb_ary_push(typelibs, typelib);
		}
            }
        }
        RegCloseKey(hguid);
    }
    RegCloseKey(htypelib);
    return typelibs;
}

Instance Method Details

#guidThe guid string.

Returns guid string which specifies type library.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
guid = tlib.guid # -> '{00020813-0000-0000-C000-000000000046}'

Returns:

  • (The guid string.)


5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
# File 'win32ole.c', line 5238

static VALUE
foletypelib_guid(VALUE self)
{
    ITypeLib *pTypeLib;
    OLECHAR bstr[80];
    VALUE guid = Qnil;
    int len;
    TLIBATTR *pTLibAttr;

    pTypeLib = oletypelib_get_typelib(self);
    oletypelib_get_libattr(pTypeLib, &pTLibAttr);
    len = StringFromGUID2(&pTLibAttr->guid, bstr, sizeof(bstr)/sizeof(OLECHAR));
    if (len > 3) {
        guid = ole_wc2vstr(bstr, FALSE);
    }
    pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
    return guid;
}

#inspectString

Returns the type library name with class name.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
tlib.inspect # => "<#WIN32OLE_TYPELIB:Microsoft Excel 9.0 Object Library>"

Returns:

  • (String)


5528
5529
5530
5531
5532
# File 'win32ole.c', line 5528

static VALUE
foletypelib_inspect(VALUE self)
{
    return default_inspect(self, "WIN32OLE_TYPELIB");
}

#library_nameObject

Returns library name. If the method fails to access library name, WIN32OLERuntimeError is raised.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
tlib.library_name # => Excel


5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
# File 'win32ole.c', line 5481

static VALUE
foletypelib_library_name(VALUE self)
{
    HRESULT hr;
    ITypeLib *pTypeLib = NULL;
    VALUE libname = Qnil;
    BSTR bstr;

    pTypeLib = oletypelib_get_typelib(self);
    hr = pTypeLib->lpVtbl->GetDocumentation(pTypeLib, -1,
                                            &bstr, NULL, NULL, NULL);
    if (FAILED(hr)) {
        ole_raise(hr, eWIN32OLERuntimeError, "failed to get library name");
    }
    libname = WC2VSTR(bstr);
    return libname;
}

#major_versionThe type library major version.

Returns the type library major version.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
puts tlib.major_version # -> 1

Returns:

  • (The type library major version.)


5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
# File 'win32ole.c', line 5318

static VALUE
foletypelib_major_version(VALUE self)
{
    TLIBATTR *pTLibAttr;
    VALUE major;
    ITypeLib *pTypeLib;
    pTypeLib = oletypelib_get_typelib(self);
    oletypelib_get_libattr(pTypeLib, &pTLibAttr);

    major =  INT2NUM(pTLibAttr->wMajorVerNum);
    pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
    return major;
}

#minor_versionThe type library minor version.

Returns the type library minor version.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
puts tlib.minor_version # -> 3

Returns:

  • (The type library minor version.)


5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
# File 'win32ole.c', line 5341

static VALUE
foletypelib_minor_version(VALUE self)
{
    TLIBATTR *pTLibAttr;
    VALUE minor;
    ITypeLib *pTypeLib;
    pTypeLib = oletypelib_get_typelib(self);
    oletypelib_get_libattr(pTypeLib, &pTLibAttr);
    minor =  INT2NUM(pTLibAttr->wMinorVerNum);
    pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
    return minor;
}

#nameThe type library name Also known as: to_s

Returns the type library name.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
name = tlib.name # -> 'Microsoft Excel 9.0 Object Library'

Returns:

  • (The type library name)


5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
# File 'win32ole.c', line 5266

static VALUE
foletypelib_name(VALUE self)
{
    ITypeLib *pTypeLib;
    HRESULT hr;
    BSTR bstr;
    VALUE name;
    pTypeLib = oletypelib_get_typelib(self);
    hr = pTypeLib->lpVtbl->GetDocumentation(pTypeLib, -1,
                                            NULL, &bstr, NULL, NULL);

    if (FAILED(hr)) {
        ole_raise(hr, eWIN32OLERuntimeError, "failed to get name from ITypeLib");
    }
    name = WC2VSTR(bstr);
    return rb_enc_str_new(StringValuePtr(name), strlen(StringValuePtr(name)), cWIN32OLE_enc);
}

#ole_typesThe array of WIN32OLE_TYPE object included the type library. Also known as: ole_classes

Returns the type library file path.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
classes = tlib.ole_types.collect{|k| k.name} # -> ['AddIn', 'AddIns' ...]

Returns:



5509
5510
5511
5512
5513
5514
5515
5516
5517
# File 'win32ole.c', line 5509

static VALUE
foletypelib_ole_types(VALUE self)
{
    ITypeLib *pTypeLib = NULL;
    VALUE classes = rb_ary_new();
    pTypeLib = oletypelib_get_typelib(self);
    ole_types_from_typelib(pTypeLib, classes);
    return classes;
}

#pathThe type library file path.

Returns the type library file path.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
puts tlib.path #-> 'C:\...\EXCEL9.OLB'

Returns:

  • (The type library file path.)


5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
# File 'win32ole.c', line 5412

static VALUE
foletypelib_path(VALUE self)
{
    TLIBATTR *pTLibAttr;
    HRESULT hr = S_OK;
    BSTR bstr;
    LCID lcid = cWIN32OLE_lcid;
    VALUE path;
    ITypeLib *pTypeLib;

    pTypeLib = oletypelib_get_typelib(self);
    oletypelib_get_libattr(pTypeLib, &pTLibAttr);
    hr = QueryPathOfRegTypeLib(&pTLibAttr->guid,
	                       pTLibAttr->wMajorVerNum,
			       pTLibAttr->wMinorVerNum,
			       lcid,
			       &bstr);
    if (FAILED(hr)) {
	pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
	ole_raise(hr, eWIN32OLERuntimeError, "failed to QueryPathOfRegTypeTypeLib");
    }

    pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
    path = WC2VSTR(bstr);
    return rb_enc_str_new(StringValuePtr(path), strlen(StringValuePtr(path)), cWIN32OLE_enc);
}

#versionThe type library version.

Returns the type library version.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
puts tlib.version #-> 1.3

Returns:



5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
# File 'win32ole.c', line 5293

static VALUE
foletypelib_version(VALUE self)
{
    TLIBATTR *pTLibAttr;
    VALUE major;
    VALUE minor;
    ITypeLib *pTypeLib;

    pTypeLib = oletypelib_get_typelib(self);
    oletypelib_get_libattr(pTypeLib, &pTLibAttr);
    major = INT2NUM(pTLibAttr->wMajorVerNum);
    minor = INT2NUM(pTLibAttr->wMinorVerNum);
    pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
    return rb_Float(make_version_str(major, minor));
}

#visible?Boolean

Returns true if the type library information is not hidden. If wLibFlags of TLIBATTR is 0 or LIBFLAG_FRESTRICTED or LIBFLAG_FHIDDEN, the method returns false, otherwise, returns true. If the method fails to access the TLIBATTR information, then WIN32OLERuntimeError is raised.

tlib = WIN32OLE_TYPELIB.new('Microsoft Excel 9.0 Object Library')
tlib.visible? # => true

Returns:

  • (Boolean)


5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
# File 'win32ole.c', line 5452

static VALUE
foletypelib_visible(VALUE self)
{
    ITypeLib *pTypeLib = NULL;
    VALUE visible = Qtrue;
    TLIBATTR *pTLibAttr;

    pTypeLib = oletypelib_get_typelib(self);
    oletypelib_get_libattr(pTypeLib, &pTLibAttr);

    if ((pTLibAttr->wLibFlags == 0) ||
        (pTLibAttr->wLibFlags & LIBFLAG_FRESTRICTED) ||
        (pTLibAttr->wLibFlags & LIBFLAG_FHIDDEN)) {
        visible = Qfalse;
    }
    pTypeLib->lpVtbl->ReleaseTLibAttr(pTypeLib, pTLibAttr);
    return visible;
}