Class: WIN32OLE_TYPELIB

Inherits:
Object
  • Object
show all
Defined in:
win32ole.c,
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'


5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
# File 'win32ole.c', line 5222

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


5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
# File 'win32ole.c', line 5001

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.)


5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
# File 'win32ole.c', line 5271

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)


5561
5562
5563
5564
5565
# File 'win32ole.c', line 5561

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


5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
# File 'win32ole.c', line 5514

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.)


5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
# File 'win32ole.c', line 5351

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.)


5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
# File 'win32ole.c', line 5374

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)


5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
# File 'win32ole.c', line 5299

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 name;
}

#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:



5542
5543
5544
5545
5546
5547
5548
5549
5550
# File 'win32ole.c', line 5542

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.)


5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
# File 'win32ole.c', line 5445

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 path;
}

#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:



5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
# File 'win32ole.c', line 5326

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)


5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
# File 'win32ole.c', line 5485

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;
}