Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of _MERGE_PROXYSTUB?

Tags:

c++

com

atl

I have generated an ATL COM object using VS2008 and the code contains references to a definition called _MERGE_PROXYSTUB (because I chose the 'Merge proxy/stub' option when I initially ran the wizard.)

What is the point of a proxy/stub? If I don't select the the merge option then I get a separate MyControlPS.DLL instead - when would this ever be used?

FWIW the control seems to register and work fine if I remove all the code surrounded by the _MERGE_PROXYSTUB defines. A debug build doesn't even define _MERGE_PROXYSTUB and it still works OK.

So, can I do without a proxy/stub?

like image 860
Rob Avatar asked Sep 18 '09 12:09

Rob


2 Answers

You need a proxy/stub if you want your COM object to be called from an application using a different threading model than your COM object.

For example, we have a plug in that gets loaded by an application that uses a particular threading model (can't remember which), but our COM object is multithreaded apartment (MTA) - so the the proxy/stub is required to marshall the data between the objects when a function call is made, while still adhering to the rules of the threading model.

If these rules are broken, then COM will either throw an exception or return a failure HRESULT such as RPC_E_WRONG_THREAD

If you don't check the merge proxy/stub option, then visual studio produces a seperate project for the proxy/stubs which get build into a seperate dll. This makes things more difficult for deployment if they are required, but you can basically just ignore them if you are not affected by threading model issues.

So you can do without proxy/stubs if the application calling the COM object is using the same threading model as your object

Larry Osterman provides a readable introduction to threading models on his blog.

like image 133
John Sibly Avatar answered Sep 30 '22 14:09

John Sibly


Also, if your interfaces contain only type-library-friendly types (BSTR, VARIANT, etc) and appear in the library block of your IDL, you can elect to have them "type library marshalled" meaning that a system-provided proxy/stub uses the meta-data from the type library.

When interfaces are put inside the library block, and DllRegisterServer is customized to register the type library (pass TRUE to XxxModule::DllRegisterServer, if I recall correctly) your interfaces will be marshalled by the system, if necessary, as described by John Sibly.

At that point, the proxy/stub isn't even used, so _MERGE_PROXYSTUB has no effect.

like image 28
Kim Gräsman Avatar answered Sep 30 '22 14:09

Kim Gräsman