Technique:Reading COM and WinRT code
| On this wiki | Facts · Code · Notes · Topics |
|---|
How COM and WinRT calls look in a Windows binary: CoCreateInstance and RoGetActivationFactory with CLSID, IID or HSTRING constants in .rdata, QueryInterface, vtable calls of the shape [[rcx]+offset], the IUnknown, IDispatch and IInspectable slots, and the registry keys that map a CLSID to its server.
Recognising a COM call
A COM object pointer points to an object whose first pointer-sized field points to a vtable (an array of function pointers shared by all instances of the class). A method call therefore compiles to two loads and an indirect call: load the vtable from [this], load the slot from [vtable + slot*pointer_size], pass this as the first argument (rcx on x64, ecx with __stdcall or the stack on x86). On x64 this is mov rax,[rcx] then call [rax+0x10] (slot 2, Release). Every method is __stdcall and returns an HRESULT in eax, so a negative result means failure. See also Technique:Graphics API vtable hooking and Library:DXGI, Library:Direct3D 11, Library:Direct3D 12, Library:Direct3D 9 for interfaces whose slot numbers are documented on the wiki.
IUnknown, IDispatch and IInspectable slots
| Slot | x64 offset | x86 offset | IUnknown | IDispatch | IInspectable |
|---|---|---|---|---|---|
| 0 | 0x00 | 0x00 | QueryInterface | same | same |
| 1 | 0x08 | 0x04 | AddRef | same | same |
| 2 | 0x10 | 0x08 | Release | same | same |
| 3 | 0x18 | 0x0C | - | GetTypeInfoCount | GetIids |
| 4 | 0x20 | 0x10 | - | GetTypeInfo | GetRuntimeClassName |
| 5 | 0x28 | 0x14 | - | GetIDsOfNames | GetTrustLevel |
| 6 | 0x30 | 0x18 | - | Invoke | first method of the interface itself |
The order comes from Wine's IDL files (unknwn.idl, oaidl.idl, inspectable.idl). So every WinRT interface has its own methods starting at slot 6, while a classic COM interface derived straight from IUnknown starts at slot 3. IInspectable is IID af86e2e0-b12d-4c6a-9c5a-d7aa65101e90 and IActivationFactory (one method, ActivateInstance, slot 6) is 00000035-0000-0000-c000-000000000046. The vtable facts of this wiki can name the interface by data.iid: a vtable found by RTTI or IID is attached to the same page.
Creating objects: CLSID and IID constants
Classic COM: CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv) (Unverified here: x64 argument registers rcx = pointer to the CLSID, rdx = outer unknown, r8d = CLSCTX, r9 = pointer to the IID, fifth argument on the stack). Both GUIDs are 16-byte constants in .rdata, stored as Data1 (4 bytes), Data2, Data3 (2 bytes each) little endian, then 8 bytes in order, so {00000000-0000-0000-C000-000000000046} (IUnknown) appears as 00 00 00 00 00 00 00 00 C0 00 00 00 00 00 00 46. Search the binary for those bytes, or for the GUID text if the code uses string forms (CLSIDFromString, StringFromGUID2 use the braced upper-case form), and the cross-references are the call sites. __uuidof(T) and IID_PPV_ARGS(&p) (riid plus ppv in one macro) just push the IID address and the out pointer. Variants to know: CoCreateInstanceEx, CoGetClassObject (then IClassFactory::CreateInstance), CLSIDFromProgID.
A second QueryInterface-shaped pattern is a function that compares the requested IID against a list and returns this adjusted for the matching base: ATL generates a table of them, see Library:ATL.
Registry layout (documented level)
Wine's combase reads these keys (dlls/combase/combase.c): CLSID\{clsid}\InprocServer32 for the in-process server DLL (CoGetClassObject tries it when CLSCTX_INPROC_SERVER is set), plus ProgID and TreatAs under the same CLSID key; failures map to REGDB_E_CLASSNOTREG when the CLSID key is missing and REGDB_E_KEYMISSING when only the server subkey is missing. Interface proxies use Interface\{iid}\ProxyStubClsid32. The type library mapping is TypeLib\{libid}\<major>.<minor>\<lcid>\win32|win64 (versions and lcid in lower-case hex without leading zeros), documented on Format:COM type library (TLB). A server DLL exports DllGetClassObject and DllCanUnloadNow. Unverified: ThreadingModel values, LocalServer32 and the HKCU overlay (not read for this page); registration-free COM uses an application manifest instead of the registry (Wine implements the lookup through activation context sections).
WinRT activation
See Platform:Windows Runtime: class names are HSTRINGs such as Windows.Foundation.Uri, passed to RoGetActivationFactory(hstring, iid, out); on failure C++/WinRT and windows-rs fall back to loading <namespace prefix>.dll and calling its DllGetActivationFactory.
C++/WinRT and WRL
C++/WinRT (microsoft/cppwinrt, MIT) produces ABI code in the winrt::impl namespace: IInspectable vtable methods are declared GetIids(uint32_t*, guid**), GetRuntimeClassName(void**), GetTrustLevel(TrustLevel*) in strings/base_abi.h. Projected classes call a get_activation_factory-style helper that wraps RoGetActivationFactory (strings/base_activation.h), so a binary shows RoGetActivationFactory imports (from combase.dll or api-ms-win-core-winrt-l1-1-0.dll, Unverified) plus WindowsCreateString / WindowsDeleteString. Unverified: WRL (Microsoft::WRL) code shows Wrappers::HStringReference, RuntimeClass<...> templates and InspectableClass(L"Namespace.Class", TrustLevel) macros; look for the class-name wide string next to a GetRuntimeClassName slot.
Code & repositories
- https://github.com/wine-mirror/wine @
7298a818b79446bae153b8d5aa27235ab3703bf9· license: LGPL-2.1-or-later (declared by the contributor; GitHub's detector did not recognize COPYING.LIB)
Wine: dlls/oleaut32/typelib.c and typelib.h (MSFT and SLTG parsers), dlls/combase/combase.c (CLSID registry lookups), include/*.idl (IUnknown, IDispatch, IInspectable, IActivationFactory).
Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-10-16 - https://github.com/microsoft/cppwinrt @
856eaff70a48d6b053c2ad3dffec15b81ca34b10· license: MIT
C++/WinRT: strings/base_abi.h (IInspectable ABI), base_activation.h (activation with DllGetActivationFactory fallback), base_implements.h.
Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-10-17
Pitfalls
- Two IIDs for one interface do not exist, but two interfaces may share a layout: identify by IID, not by slot count.
- Methods marked
[local]or[call_as]in IDL appear under the local name in headers; the proxy calls the Remote* variant. - Out parameters (
void**) are written by the callee: the vtable pointer appears only after the call returns.
Related
Platform:Windows Runtime, Format:COM type library (TLB), Format:WinRT metadata (WinMD), Library:ATL, Format:.NET metadata (ECMA-335).