Home : COM / OLE / ActiveX notes : Creating a COM object with ATL
You first need a COM "server" normally a DLL file which can instantiate (create) your COM object(s). In Visual C++ version 6:
The AppWizard consists of a single step.
You now need to add one (or more) COM classes to your server. These classes define the COM objects provided by the server. In the COM server project:
The object Objects | Simple Object provides a minimal COM class.
The object Controls | Full Control provides an ActiveX control, with lots of interfaces provided and implemented for you automatically.
Short Name: The name of the C++ class that is the COM class. Other names on the property page are filled in automatically, but can be manually changed.
Interface: the name of the first COM interface supported by your COM object.
Type: a human readable name for the COM object.
Prog ID: the ProgID for the COM object. Client code can use the ProgID to specify your COM object, as an alternative to the CLSID.
Threading Model, select one of:
Interface: select Custom if Automation support isnt required.
Aggregation: select No if the COM object will never be aggregated by another object.
Only available for ActiveX controls.
Windowed Only: If checked, the ActiveX control always creates its own window for its graphical display. If unchecked, the ActiveX control supports "windowless" activation, where it draws directly into its parents window (which must supply a Device Context to the control whenever it needs to be redrawn).
Generate an IID (i.e. a new GUID) for the interface, for example by using the GUIDGEN.EXE application supplied with Visual Studio.
Open the projects IDL file. Add a new section defining the new interface (you dont need to define the methods yet), entering the new IID in the uuid section:
[
object,
uuid(12345678-1234-1234-1234-123456789ABC),
helpstring("INewInterface Interface"),
pointer_default(unique)]
interface INewInterface : IUnknown
{
};
Also, add the new interface to the type library for the COM class:
library NAME_OF_DLLLib
{coclass MyComObject
{interface INewInterface;
};
};
Re-build the project. Then, in "ClassView", right click on your COM class, and select "Implement Interface " from the context menu. In the dialogue box that appears, check the interface you want to add, then click on OK.
In "ClassView", right click on the interface, and select "Add Method " from the context menu. In the dialogue box that appears, enter the methods name, list its parameters (using IDL syntax, so you can use [in] and [out] attributes), then click on OK.
In "ClassView", right click on your COM class, and select "Implement Interface " from the context menu. In the dialogue box that appears, click on "Add Typelib ", click on "Browse ", and find the type library for the project where the interface was defined. Check the interface(s) you want to add, then click on OK.