Enabling proxy/stub code in an ATL control

By default, an ATL project does not build the proxy/stub code that is required for marshalling. Marshalling is needed when a control is created in an incompatible apartment, requiring the control to be created in a different apartment to the creator. If the proxy/stub code has not been registered, the object creation (e.g. CoCreateInstance) will fail with the error code 0x80040155 (REGDB_E_IIDNOTREG, "interface not registered").

A new ATL project is typically created using the "ATL COM AppWizard" of Visual C++. When using the wizard, make sure that the option "Allow merging of proxy/stub code" is checked.

A control is typically added to the ATL server project using the "Insert | New ATL Object…" menu option of Visual C++.

Follow the steps listed in the "project_name.cpp" file to enable the proxy/stub code to be built at the same time as the server code:

Select "Project | Settings…". In the tree at the left of the dialogue box, expand "Source Files" and select "dlldatax.c". Uncheck the "Exclude file from build" option.

Still with the "dlldatax.c" file selected, click on the C/C++ tab. Select the "Category" of "Precompiled Headers". Select "Not using precompiled headers".

Select the root of the tree, to select the entire project. Under the C/C++ tab, select the "Category" of "Preprocessor". Add "_MERGE_PROXYSTUB" to the list of "Preprocessor definitions".

Now whenever you build the project, the proxy/stub code will also be compiled and registered.