MSDN Documentation

COM Objects

This section of the Windows API Reference provides comprehensive documentation for Component Object Model (COM) objects. COM is a binary-interface standard for the creation of reusable software components. It is the foundation for many Microsoft technologies, including OLE, ActiveX, and DirectX.

Introduction to COM Objects

COM objects are instances of COM classes that expose interfaces. These interfaces define a set of functions (methods) that clients can call to interact with the object. The power of COM lies in its language independence and binary reusability. Developers can create components in one language and use them in applications written in another, as long as they adhere to the COM specification.

A COM object is characterized by its Class Identifier (CLSID), which uniquely identifies the object's class, and its Interface Identifier (IID), which identifies a specific interface that the object supports.

Key Concepts

  • Interfaces: A contract between the COM object and its clients. Defined by a set of function pointers.
  • vtable (Virtual Table): A table of function pointers that represents an interface. COM objects implement interfaces by providing a vtable.
  • Reference Counting: A mechanism to manage the lifetime of COM objects. Clients call AddRef to increment the reference count and Release to decrement it. The object is destroyed when the reference count reaches zero.
  • Aggregation: A mechanism for one COM object to contain another and expose its interfaces to clients.
  • Monikers: Objects that can bind to other objects, providing a way to locate and activate COM objects.

Commonly Used COM Objects

The Windows operating system provides a vast array of COM objects that applications can leverage. Here are some fundamental ones:

IUnknown

The foundational interface of all COM objects. It provides methods for reference counting (AddRef, Release) and querying for other interfaces (QueryInterface).

interface IUnknown {
    virtual HRESULT QueryInterface(
        REFIID riid,
        void **ppvObject
    ) = 0;

    virtual ULONG AddRef() = 0;
    virtual ULONG Release() = 0;
};

IDispatch

An interface that allows late-bound access to an object's properties and methods, enabling dynamic invocation and automation.

interface IDispatch : IUnknown {
    // ... methods for type information and invocation
};

IPersistFile

An interface for COM objects that can be loaded from and saved to files.

interface IPersistFile : IPersist {
    // ... methods for saving and loading from files
};

API Details and Examples

Below are details for specific COM objects and their associated APIs. Each entry includes a brief description, the primary interface(s), and relevant code snippets or links to more detailed documentation.

File System Object (FSO)

Represents files, folders, and drives, allowing manipulation of the file system.

Primary Interface: FileSystemObject (often accessed via Scripting.FileSystemObject ProgID)

Example: Creating a Text File
// VBScript Example
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("C:\temp\myfile.txt", True)
file.WriteLine("Hello, COM!")
file.Close
Set file = Nothing
Set fso = Nothing

Registry Manipulation Interfaces

Interfaces for interacting with the Windows Registry.

Primary Interfaces: IRegistryKey, IRegistryValue (hypothetical, actual interfaces may vary based on specific COM components)

Example: Reading a Registry Value (Conceptual C++)
// C++ Conceptual Snippet
#include <windows.h>
#include <atlbase.h> // For CComPtr

// Assuming existence of a COM object with registry access
CComPtr<IRegistryReader> spRegReader;
HRESULT hr = CoCreateInstance(CLSID_RegistryReader, NULL, CLSCTX_INPROC_SERVER, IID_IRegistryReader, (void**)&spRegReader);

if (SUCCEEDED(hr)) {
    CComVariant value;
    hr = spRegReader->GetValue(L"SOFTWARE\\MyApp", L"SettingName", &value);
    if (SUCCEEDED(hr)) {
        // Use the value
    }
}

Shell Objects

Provides access to Windows Shell functionality, such as browsing folders, creating shortcuts, and managing the shell namespace.

Primary Interface: IShellFolder

Example: Getting the Desktop Folder (Conceptual C++)
// C++ Conceptual Snippet
#include <shlobj.h>

IShellFolder *pDesktopFolder = NULL;
SHGetDesktopFolder(&pDesktopFolder);

if (pDesktopFolder) {
    // Use pDesktopFolder to interact with the desktop
    pDesktopFolder->Release();
}