This article provides a programming guide to the programmers who just contacted COM, explaining the inside of the COM server and how to write your own interface in C + +.
Following the introduction of previous COM programming, this article discusses the content of COM servers, explains the steps and knowledge needed to write your own COM interfaces and COM servers, and discusses in detail the internal mechanisms that COM servers run when COM libraries make calls to COM servers.
If you have read the previous article. Should be very familiar with how COM clients are going to do it. This article discusses the other end of COM--com servers. The content includes how to write a simple COM server that does not involve the class library in C + +. Deep into the internal process of creating a COM server, it is the best way to fully understand the internal mechanism of a COM server without obscuring the library code.
This article assumes that you are proficient in C + + and have mastered the concepts and terminology discussed in the previous article. In this section it will include the following elements:
A glance at a COM server-describes the basic requirements of a COM server.
Server Life Management--Describes how the COM server controls load time.
Implement the interface, starting with IUnknown--showing how to write an interface with the C + + class to implement and describe the purpose of the IUnknown method.
Delve into CoCreateInstance ()--Explore the calling mechanism of CoCreateInstance ().
COM Server Registration--describes the registry entry required to complete the server registration.
Creating a COM Object-class factory-describes the process of creating a COM object to be used by the client.
An example of a custom interface--the example code illustrates the concept above.
A client using the server--an example of a simple client application that tests a COM server.
Other content-Comments about source code and debugging.
A glance at a COM server
In this article we will discuss the simplest kind of COM server, in-Process server (in-process). In-process means that the server is loaded into the process space of the client program. In-process servers are DLLs and are on a single computer with the client program.
In-process servers must meet two criteria or criteria before being used by a COM library:
1, must correctly register under the HKEY_CLASSES_ROOT\CLSID key value of the registry.
2, must output DllGetClassObject () function.
This is the minimum requirement for running in-process servers. A key value must be created under the HKEY_CLASSES_ROOT\CLSID key value of the registry, using the server's GUID as the key name, which must contain a list of two key values, the location of the server, and the server's threading model. The COM library's invocation of the DllGetClassObject () function is done in the CoCreateInstance () API.
There are also three functions that are usually output:
o DllCanUnloadNow (): A COM library call to check whether the server is unloaded from memory.
o DllRegisterServer (): The installation utility, similar to RegSvr32, is used to register the server.
o DllUnregisterServer (): Used by the uninstall utility to delete registry entries created by DllRegisterServer ().
In addition, it is not enough to output only the correct functions-you must also follow the COM specification so that the COM library and client programs can use the server.