2

Can I use the WRL library in C++ as a replacement for ATL to write a COM component? And if yes, would I be able to use it on older desktop Windows systems, like Windows XP?

I'm pretty sure that the answer to the first question is positive as I found this tutorial on MSDN:

http://msdn.microsoft.com/en-us/library/jj822931.aspx

but what about non-Windows 8 systems?

szx
  • 6,433
  • 6
  • 46
  • 67

2 Answers2

8

Well, sure you can. You can write a COM server in pure C++ code without any helper classes at all. You can write one in C if you really want to, although that's a violation of the Geneva Convention on Programmer's Rights in most jurisdictions.

What is probably not so visible is what is missing. WRL just doesn't make it any easier to write a server. Or maintain it. What you'll miss out on:

  • No help whatsoever to implement IDispatch. Obsoleted in WinRT but still quite important in regular servers. The only way to consume the server from a scripting language, IDispatch provides the late binding support.
  • No help whatsoever for registration. Obsoleted in WinRT, required for regular servers. Note how the DllRegisterServer() entrypoint is missing. The page you linked patches around the problem by requiring you to write a registry script by hand. That works, it is not exactly something you'd want to maintain or document so IT staff can get it right.
  • No wrappers for Automation types like BSTR, VARIANT and SAFEARRAY. Obsoleted in WinRT along with IDispatch. You can still fall back to <comutil.h> however.
  • No help whatsoever from the wizards that are built into Visual Studio that help you get it right. COM servers are awkward because definitions appear in multiple places in your code. And need to be an exact match. You get no help to ensure that CalculatorComponent is an exact match with the IDL, it is entirely up to you. Compiler errors you get from making small mistakes, particularly when maintaining it, can be painful.

And a whole bunch if smaller stuff not worth mentioning, like apartments, ActiveX, aggregation, error info, etc. Hurts pretty bad when you need it though. A possible advantage of WRL is that there's less mystical glue, ATL has a fair amount of it that considerably raises the abstraction level. That was intentional but it has to be learned. There's none at all when you use pure C++ of course, albeit that you have to write too much of it yourself.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Yes. You can write a standard COM component. There is a sample for this directly in the docs.

And no: Such a COM component will only run on Windows 8 and later...

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Why would it only run under Windows 8? – CÅdahl Nov 07 '14 at 16:31
  • Because the WRL (Windows Runtime Library) was first presented with Windows 8. – xMRi Nov 07 '14 at 21:50
  • It is however a header based template library. So as long as you have it (and VS2012+) there should be no problem deploying to older OS versions if you just use the ClassicCom parts. – CÅdahl Nov 08 '14 at 09:03
  • And why should some one do this when the target DLLs and classes are all missing? – xMRi Nov 08 '14 at 13:13
  • 1
    What target DLL and classes? The question concerns base level COM, I see no mention of calling WinRT-specific APIs. – CÅdahl Nov 08 '14 at 14:05
  • FWIW, just finished a project involving a COM library written with WRL, targeting Vista and up. Did end up having a dependency on the VC++ 2013 runtime redistributable, but that's par for the course with or without WRL. Didn't end up using the class factories, opted for simple exported factory functions in the style of DirectX instead. But the end result works nicely, interops with .NET etc. – CÅdahl Dec 15 '14 at 06:24
  • From what I can see, implementing a COM Module in WRL links against runtimeobject.lib and takes a runtime dependency on the winrt-error API set, so how did you get this to work on Vista and Win7? Edit: I see, by not using the class factories. Ok... not sure what value WRL is providing over ATL / handwritten, in that case. – Daniel Strommen Apr 10 '18 at 21:58