IMoniker
A moniker is a system object that identifies a COM object. Win32 has several functions for creating them, see references.
A moniker is bound to its target in a binding context, an object that implements IBindCtx.
Basic Usage
Monikers usually work under the hood. CoGetObject is a convenience function that retrieves an object from its display name. It calls MkParseDisplayName to get the moniker, then IMoniker::BindToObject to get the underlying object. Here’s some pseudo code for that.
Class Moniker
One use case of monikers is when you want to create an object with ‘constructor’ arguments. Internally, CoCreateInstance is a thin wrapper that calls CoGetClassObject, passing in the IID of the default IClassFactory, which does not support arguments.
We can work around this in C++ by calling CoGetClassObject directly and passing in the IID of a custom class factory:
But to get to the factory interface from a scripting language, a moniker is needed. First the C++:
The corresponding script looks something like this:
References
- CreateFileMoniker function (objbase.h) - Win32 apps | Microsoft Learn
- CreateItemMoniker function (objbase.h) - Win32 apps | Microsoft Learn
- CreatePointerMoniker function (objbase.h) - Win32 apps | Microsoft Learn
- CreateAntiMoniker function (objbase.h) - Win32 apps | Microsoft Learn
- CreateGenericComposite function (objbase.h) - Win32 apps | Microsoft Learn
- CreateClassMoniker function (objbase.h) - Win32 apps | Microsoft Learn
- URL Moniker Functions - Win32 apps | Microsoft Learn
- CreateObjrefMoniker function (objbase.h) - Win32 apps | Microsoft Learn
- MkParseDisplayName function (objbase.h) - Win32 apps | Microsoft Learn
- CoGetObject function (objbase.h) - Win32 apps | Microsoft Learn