Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TypeLib Explained?

46 views
Skip to first unread message

BeeJ

unread,
Apr 22, 2013, 6:51:58 PM4/22/13
to
I have been reading note on TypeLibs and these notes seem to add some
"definitions" beyond other articles I have read.
Basically I understand that TypeLib may contain many Declare (etc)
definitions that are then not necessary in the source. This eliminates
the need to write .BAS modules etc to provide Declares etc. TypeLibs
"interfaces" are compiled in so the TypeLib.tlb are not required to tag
along with the compiled code delivery. That is a good thing.

But what I also have seen in notes that having the Declares in TypeLibs
provide a little insurance in for example a callback because ...
because of what? It was stated that calling API Declares would cause a
crash while using the TypeLib call would not. The particular code is
using CopyMemory and SendMessage from the TypeLib.

This little hint has helped me solve a crash problem I have been having
in a callback. Now I think I know why and I also now know how to make
the code play ... maybe.

What other advantages do TypeLibs provide.


ralph

unread,
Apr 22, 2013, 9:12:25 PM4/22/13
to
On Mon, 22 Apr 2013 15:51:58 -0700, BeeJ <spa...@nospam.com> wrote:


>
>What other advantages do TypeLibs provide.
>

For "WinAPI-type" DLLs, a definition in a type library provides
slightly better performance as a Declare directive carries with it
some overhead.

Using a Declare directive VB introduces additional (hidden) parameter
testing and for lack of a better term - "existence checking". A
typelib eliminates this as this is all done at 'development time'.
This is also often referred to as 'type safety'.

By creating a unique 'library' for each DLL you can make it slightly
easier to resolve name-collisions by simply providing a complete
reference. A la ..
LibA.AddItem()
LibB.AddItem()
Which can occasionally come about in a busy development environment.
[You do this with the Declare directive by using Alias.]

You can also declare Constants, Enums, and UDTs.

Declaring UDTs in a "global" type library allows you to pass them
between classes in VB.

When declaring and defining these items in VB you often have to repeat
the declarations in more than one module. This introduces multiple
opportunities for error. Using a type library allows one to declare
them only once, and in only one place.

Need to change something - go to one IDL file, not employing a
search/replace.

Using a type library you can also provide help information which will
show up in Intellisense.

If you are using a mixed environment such as VC++, Delphi, VB, ...
instead of having different definitions (eg, a header file in one,
Declares in one format or another) all of them can use a type library
for your shared items.

Probably missing something ... <g>

-ralph

Deanna Earley

unread,
Apr 23, 2013, 4:08:59 AM4/23/13
to
On 22/04/2013 23:51, BeeJ wrote:
> I have been reading note on TypeLibs and these notes seem to add some
> "definitions" beyond other articles I have read.
> Basically I understand that TypeLib may contain many Declare (etc)
> definitions that are then not necessary in the source. This eliminates
> the need to write .BAS modules etc to provide Declares etc. TypeLibs
> "interfaces" are compiled in so the TypeLib.tlb are not required to tag
> along with the compiled code delivery. That is a good thing.
>
> But what I also have seen in notes that having the Declares in TypeLibs
> provide a little insurance in for example a callback because ... because
> of what? It was stated that calling API Declares would cause a crash
> while using the TypeLib call would not. The particular code is using
> CopyMemory and SendMessage from the TypeLib.

Not callbacks in general, it's a callback on another thread where it helps.
In that case, it allows you to call a few API functions, but you can't
go beyond that due to uninitialized Thread Local Storage (TLS).

> What other advantages do TypeLibs provide.

Unicode/wide string support.
Declared functions implicitly marshal string parameters to ANSI strings.

--
Deanna Earley (dee.e...@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be ignored. Please reply to the
group.)
Message has been deleted

ralph

unread,
Apr 23, 2013, 8:46:46 AM4/23/13
to
On Tue, 23 Apr 2013 11:56:25 +0200, Arne Saknussemm
<motz001.2...@spamgourmet.com> wrote:

>
>> eliminates the need to write .BAS modules etc to provide Declares
>> etc. TypeLibs "interfaces" are compiled in so the TypeLib.tlb are
>> not required to tag along with the compiled code delivery. That is a
>> good thing.
>
>I think Ralph and Deanna covered pretty much the topic; so I'll just
>add a link to a windows API typelib which may be of help in case you
>want to use a number of "common" APIs in your code
>
>http://webspace.webring.com/people/lp/practicalvb/vb/download/win32.html
>

Another excellent source is Bruce McKinney's Win32 IDL source files.

http://vb.mvps.org/hardweb/mckinney2a.htm#19

Download WinTlb3.zip and WinTlbU.zip.

McKinney's book and followup article, while definitely dated, is still
the most complete coverage on how (and why) to create type libraries
for VB. If you do build McKinney's ANSI typlib - rename it so it
doesn't collide with Rising Productions.

-ralph

Jim Mack

unread,
Apr 23, 2013, 9:09:37 AM4/23/13
to
>
>> What other advantages do TypeLibs provide.
>
> Unicode/wide string support.
> Declared functions implicitly marshal string parameters to ANSI strings.

The ability to pass arrays "As Any". Since a SafeArray contains some OLE
type information, this allows you to write generic array-handling
procedures.

Also true, though less useful, for other parameter types.

--
Jim

Jim Mack

unread,
Apr 23, 2013, 9:11:49 AM4/23/13
to
>
> You can also declare Constants, Enums, and UDTs.

Except, oddly, double precision floating point Consts.

--
Jim

ralph

unread,
Apr 23, 2013, 12:07:25 PM4/23/13
to
On Tue, 23 Apr 2013 09:11:49 -0400, Jim Mack <no-ub...@mdxi.com>
wrote:

>>
>> You can also declare Constants, Enums, and UDTs.
>
>Except, oddly, double precision floating point Consts.

Not too odd as this is by design, but worth mentioning.

The MIDL allows constant integer, character, string, and Boolean types
within an interface. The reason is they are treated as literal
#defines - even VB has trouble with specific literals without type
identifier characters.

It is useful to note that the IDL specification itself does provide a
broader range of types, but the handling of those types is purely
implementation and compiler defined.

-ralph

ralph

unread,
Apr 23, 2013, 12:15:30 PM4/23/13
to
On Tue, 23 Apr 2013 09:09:37 -0400, Jim Mack <no-ub...@mdxi.com>
wrote:

>>
>>> What other advantages do TypeLibs provide.
>>
>> Unicode/wide string support.
>> Declared functions implicitly marshal string parameters to ANSI strings.
>
>The ability to pass arrays "As Any". Since a SafeArray contains some OLE
>type information, this allows you to write generic array-handling
>procedures.
>

You need to be clearer on exactly what you mean here. Since depending
on "As Any" is exactly what one hopes to avoid by employing a type
library.

-ralph

Karl E. Peterson

unread,
Apr 23, 2013, 12:32:17 PM4/23/13
to
Deanna Earley explained :
>> What other advantages do TypeLibs provide.
>
> Unicode/wide string support.
> Declared functions implicitly marshal string parameters to ANSI strings.

Unless you just pass the pointers back and forth. :-)

--
.NET: It's About Trust!
http://vfred.mvps.org


Jim Mack

unread,
Apr 23, 2013, 1:13:33 PM4/23/13
to
On 4/23/2013 12:07 PM, ralph wrote:
> On Tue, 23 Apr 2013 09:11:49 -0400, Jim Mack <no-ub...@mdxi.com>
> wrote:
>
>>>
>>> You can also declare Constants, Enums, and UDTs.
>>
>> Except, oddly, double precision floating point Consts.
>
> Not too odd as this is by design, but worth mentioning.

Not by design but a flaw in VB, noted in the KB. VB can consume float
consts, but not doubles. Those compile OK but they show up as weird text
values when used in VB. I discovered this when I included literal values
for some transcendentals (pi, e, etc) in the Stamina typelib. Too bad,
it would have been useful.

--
Jim

BeeJ

unread,
Apr 23, 2013, 1:15:14 PM4/23/13
to
ralph was thinking very hard :
That is very helpful. Thank you!


BeeJ

unread,
Apr 23, 2013, 1:19:29 PM4/23/13
to
Deanna Earley explained on 4/23/2013 :
>
> Not callbacks in general, it's a callback on another thread where it helps.
> In that case, it allows you to call a few API functions, but you can't go
> beyond that due to uninitialized Thread Local Storage (TLS).
>
Yes, that was what I meant to say. The API I am talking about is on
another thread.

But why? Faster? Smaller? Or both? Or ... ?

Currently I use a timer to disconnect from the callback.
That seems to work. Is there any other method?

>> What other advantages do TypeLibs provide.
>
> Unicode/wide string support.
> Declared functions implicitly marshal string parameters to ANSI strings.

Yes, I have seen that.


BeeJ

unread,
Apr 23, 2013, 1:22:18 PM4/23/13
to
ralph pretended :
I already have McKinneys typelibs. Thanks to Karl.

I will check out the link Arne provided.


BeeJ

unread,
Apr 23, 2013, 1:26:53 PM4/23/13
to
So far so good.

Where can I find methods to easily create typelibs?
I see ODL and IDL and whatever else there is.

I know that an ActiveX can produce a .TLB by setting a switch.
Any writeups on that method?

Is there a free builder that I can start with VB6 source (other than an
ActiveX) like a .BAS and produce a TypeLib?


BeeJ

unread,
Apr 23, 2013, 1:29:29 PM4/23/13
to
on 4/23/2013, BeeJ supposed :
Does Tlbexp.exe do the same as using VB6 on a .DLL and setting that
switch?


Jim Mack

unread,
Apr 23, 2013, 1:35:05 PM4/23/13
to
>> The ability to pass arrays "As Any". Since a SafeArray contains some OLE
>> type information, this allows you to write generic array-handling
>> procedures.
>>
>
> You need to be clearer on exactly what you mean here. Since depending
> on "As Any" is exactly what one hopes to avoid by employing a type
> library.

Simple -- you can write (for example) a generic array insert or array
sort function using the TLB interface that would be impossible using the
Declare interface.

Like just about anything we caution against (GoTo, global vars etc), As
Any does have its uses. I would never use it in a typelib for an API
that doesn't know what to do with it, but when you're in control of the
implementation, "SAFEARRAY(void) * myArray" is valid and useful.

I've written dozens of routines that work this way, used by thousands of
programmers, with appropriate safeguards built in.

--
Jim

ralph

unread,
Apr 23, 2013, 2:49:54 PM4/23/13
to
On Tue, 23 Apr 2013 13:35:05 -0400, Jim Mack <no-ub...@mdxi.com>
wrote:
I think we are crossed on just who is chewing on and defining who. <g>

I misunderstood your "As Any" reference as one can easily declare an
OLE SAFEARRAY in a type library and remove doubt. It sounded like you
were giving preference to an "As Any" in a Declare directive.

-ralph

ralph

unread,
Apr 23, 2013, 2:59:50 PM4/23/13
to
Take McKinney's source as use as a template.

Type Libraries follow a very well defined structure.

30 minutes with his source will provide a good introduction.

copy his and then change out the names and add your own declares.
Use his has a guide.

-ralph

Jim Mack

unread,
Apr 23, 2013, 3:59:50 PM4/23/13
to
>
> I misunderstood your "As Any" reference as one can easily declare an
> OLE SAFEARRAY in a type library and remove doubt. It sounded like you
> were giving preference to an "As Any" in a Declare directive.

Not me, that's crazy talk. (-:

I just meant that you could do the equivalent of "Array() As Any" in a
typelib, while you can't in a Declare. As long as you take care, it's a
useful construct and can simplify things for the caller.

--
Jim

CoderX

unread,
Apr 23, 2013, 4:09:24 PM4/23/13
to

Once again, Google eldues you.

"BeeJ" <spa...@nospam.com> wrote in message
news:kl4euf$de1$1...@speranza.aioe.org...

CoderX

unread,
Apr 23, 2013, 4:15:09 PM4/23/13
to

"ralph" <nt_con...@yahoo.com> wrote in message
news:cvldn81fgngkjv2cq...@4ax.com...
>
> Take McKinney's source as use as a template.
>
> Type Libraries follow a very well defined structure.
>
> 30 minutes with his source will provide a good introduction.
>
> copy his and then change out the names and add your own declares.
> Use his has a guide.
>

What a prefect answer for the OP. Take someone elses code, modify it and
use it with no idea how it works or what it does and then brag about being
an 'expert' in things 'others rarely do.'

I know the bar has fallen as of late, but I never expected it to become
buried.


ralph

unread,
Apr 23, 2013, 8:51:48 PM4/23/13
to
Sorry. My bad. Should have told him to Google it.

-ralph
<g>

BeeJ

unread,
Apr 23, 2013, 9:08:09 PM4/23/13
to
lol


BeeJ

unread,
Apr 23, 2013, 9:09:00 PM4/23/13
to
Seek professional help.


BeeJ

unread,
Apr 23, 2013, 9:10:16 PM4/23/13
to
Arne Saknussemm pretended :
>> eliminates the need to write .BAS modules etc to provide Declares
>> etc. TypeLibs "interfaces" are compiled in so the TypeLib.tlb are
>> not required to tag along with the compiled code delivery. That is a
>> good thing.
>
> I think Ralph and Deanna covered pretty much the topic; so I'll just
> add a link to a windows API typelib which may be of help in case you
> want to use a number of "common" APIs in your code
>
> http://webspace.webring.com/people/lp/practicalvb/vb/download/win32.html

Thank you!


Schmidt

unread,
Apr 25, 2013, 11:11:28 AM4/25/13
to
Am 23.04.2013 21:59, schrieb Jim Mack:

> I just meant that you could do the equivalent of "Array() As Any" in a
> typelib, while you can't in a Declare. As long as you take care, it's a
> useful construct and can simplify things for the caller.

At least for VB6 - the Declares below will work...
but not sure, if you meant that.

Declare Sub BindArray Lib "kernel32" Alias "RtlMoveMemory" _
(PArr() As Any, pSrc&, Optional ByVal CB& = 4)
Declare Sub ReleaseArray Lib "kernel32" Alias "RtlMoveMemory" _
(PArr() As Any, Optional pSrc& = 0, Optional ByVal CB& = 4)


Olaf

Jim Mack

unread,
Apr 25, 2013, 11:41:09 AM4/25/13
to
Well, now I don't know what I meant. If that changed from VB4 or 5 then
I wasn't aware of it -- I remember it solved a problem in VB4 and I
never looked back, but the memory is fuzzy.

In any case there are almost no Win APIs that it would be useful for.
You may have found the only one.

--
Jim

ralph

unread,
Apr 25, 2013, 1:00:48 PM4/25/13
to
On Thu, 25 Apr 2013 11:41:09 -0400, Jim Mack <no-ub...@mdxi.com>
wrote:

>On 4/25/2013 11:11 AM, Schmidt wrote:
>> Am 23.04.2013 21:59, schrieb Jim Mack:
>>
>>> I just meant that you could do the equivalent of "Array() As Any" in a
>>> typelib, while you can't in a Declare. As long as you take care, it's a
>>> useful construct and can simplify things for the caller.
>>
>> At least for VB6 - the Declares below will work...
>> but not sure, if you meant that.
>>
>> Declare Sub BindArray Lib "kernel32" Alias "RtlMoveMemory" _
>> (PArr() As Any, pSrc&, Optional ByVal CB& = 4)
>> Declare Sub ReleaseArray Lib "kernel32" Alias "RtlMoveMemory" _
>> (PArr() As Any, Optional pSrc& = 0, Optional ByVal CB& = 4)
>
>Well, now I don't know what I meant. If that changed from VB4 or 5 then
>I wasn't aware of it -- I remember it solved a problem in VB4 and I
>never looked back, but the memory is fuzzy.
>

Array handling did change from from VB5 to VB6, not sure about subtle
changes on Array parameters, but with VB6 - Functions and Properties
could return arrays, and you can copy one array into another if the
target array is a dynamic (non-fixed) array. VB5 did not allow this.

-ralph

Schmidt

unread,
Apr 26, 2013, 2:17:29 AM4/26/13
to
Am 25.04.2013 19:00, schrieb ralph:

> Array handling did change from from VB5 to VB6, not sure about subtle
> changes on Array parameters, but with VB6 - Functions and Properties
> could return arrays, and you can copy one array into another if the
> target array is a dynamic (non-fixed) array. VB5 did not allow this.

Yep, that could explain it - didn't try SafeArray-stuff in the
VB4/5-era (still being a Newbie at that time).


Olaf

Clive Lumb

unread,
Apr 26, 2013, 4:11:16 AM4/26/13
to
"BeeJ" <spa...@nospam.com> a ᅵcrit dans le message de groupe de discussion
: kl7bbc$qtp$1...@speranza.aioe.org...
> Seek professional help.
>
He is doing, why else do you think that he keeps on posting here?

BeeJ

unread,
Apr 26, 2013, 1:14:21 PM4/26/13
to
He never seems to ask or answer just poke.


ralph

unread,
Apr 29, 2013, 10:14:57 AM4/29/13
to
On Fri, 26 Apr 2013 08:17:29 +0200, Schmidt <n...@vbRichClient.com>
wrote:
Actually you were using them all the time. A VB Array is implemented
as an (OLE) SafeArray. <g>

Coming to VB from C/COM I was well aware of that - which led to all
sorts of confusion (being a VB newbie myself) - until I learned to
ignore that fact and just let VB do its thing. <bg>

-ralph
0 new messages