Tuesday, February 13, 2007

Using LoadTypeLib and LoadTypeLibEx

When most people use COM they stumble through it and if it works they just go with it. If you don't stumble with COM you probably can ignore this post but somehow I'd be willing to bet you'll read on. There are little idiosyncrasies in COM, ActiveX or COM Interop that aren't documented, so here's a little piece of information about type libraries. To load a type library into memory and get a pointer to the ITypeLib interface use the function LoadTypeLib.

HRESULT LoadTypeLib(const OLECHAR FAR *szFile, ITypeLib FAR* FAR *pptlib);

LoadTypeLib is the foundation of COM. Any call to LoadTypeLib will register the type library if it is not already registered. That's why Microsoft invented this function:

HRESULT LoadTypeLibEx(LPCOLESTR szFile, REGKIND regkind, ITypeLib** pptlib);

Any function is better with an "Ex" on the end. If you just want to go spelunking around in a type library and don't want it registered then use LoadTypeLibEx and pass in REGKIND_NONE as the regKind parameter. Problem is if your type library references other type libraries and they are in the current directory and they are not registered then they will be after this call, but the type library you passed in as the szFile parameter won't get registered.


Ed Graham said...

Cheers -- this helped me during my revision for Microsoft's 70-536 exam.

Chris Bensen said...

@Ed, glad it helped!

Post a Comment