Clients
Now that you have a distributed component in place, you can make use of its exposed COM objects. For simplicity, the term client refers to any piece of code that makes use of a COM object’s interface pointer. This implies that a COM object may be a client to another COM object. In other words, any COM object can be both a client and a server at the same time. It is this ability that allows maximum distributed capabilities.
In this section, we’ll examine the basics for using services of a distributed COM object. You’ll learn how to instantiate a distributed object, use it, and release the acquired resources.
Initialization and Termination
Recall that before a piece of code can use COM, it must first initialize the COM library. A client can call CoInitialize or CoInitializeEx to initialize the library. To be precise, any thread that uses COM must do this. This means that if your application spawns ten threads that use COM, all must call CoInitialize or CoInitializeEx. After you initialize COM, you may create, use, and release COM objects. When you’re all done, you should call CoUninitialize.
Creating an Object
To use a distributed COM object, you must first instantiate it. There are several ways to instantiate COM objects in COM, and we will discuss four common ones. Each of these techniques has advantages and disadvantages.
CoCreateInstance
The easiest way to create a COM object is by using the CoCreateInstance COM API function:
HRESULT CoCreateInstance( REFCLSID rclsid, // CLSID ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access