News for hackers. Stuff that matters.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by ChrisSD on Friday May 10, 2019 @09:25PM (#19883873)
COM isn't C++. It's a language independent interface. There's no reason why COM (and therefore UWP) can't be used from C.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by userbinator on Friday May 10, 2019 @09:54PM (#19884012)
It can be used from C but is a huge pain that makes it obvious whoever designed the interface (or COM in general) really went off the deep end with the abstraction level. There's a major difference between a C API designed to be used from C (Win32, POSIX) and one that only happens to be usable from C.

Compare a sane "C API designed to be used from C":

https://docs.microsoft.com/en-us/windows/desktop/dlgbox/usin...

that involves nothing more than initialising a structure and calling a single function, with MS's "recommended replacement" using COM:

https://msdn.microsoft.com/en-us/library/Bb776913(v=VS.85).a...

That is a ten level deep nested if with just as many function calls, to do essentially the equivalent of the code above. The first time I saw this, I seriously thought they were taking the piss.

Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by wildmusings on Saturday May 11, 2019 @09:26AM (#19886038)
It’s funny that they use that awful pyramid-of-doom error handling style in a lot of the public documentation.

The correct way is to have an “Error:” label at the end of the function before all the cleanup code. After calling any function that can return an error code, you check the error code and “goto Error” if necessary. You can encapsulate that whole logic in a macro so you just call something like CHK(hr) anytime you have an error code to check. CHK being defined as something like “if(FAILED(hr)) goto Error;”. This is how error handling for non-trivial functions is done in any serious C code, regardless of the platform. One common pattern is actually to just put the entire line of code inside the macro, so your code would look like CHK(hr = AWin32Function(param1, param2)). You can get real cute and even have the macro assign hr for you, so you can drop the “hr =“ from your code.

I worked on Windows at Microsoft and with very few exceptions, the only time I saw the pyramid-of-doom was in the public documentation. I wonder if was just bowing to the irrational hostility some people have to any use of goto, even though this is a highly structured pattern that should be uniform across your codebase, the opposite of unstructured, ad-hoc uses of goto that are the actual problem.

Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by userbinator on Saturday May 11, 2019 @02:38PM (#19887655)
True, the error handling could be better, but my main point was really to say that much of that code should not even be necessary; the majority of those calls are really just doing the equivalent of setting a field in a structure in the old way.

Furthermore, because it's a function call and "could" fail, "best" (more like dogmatic) practice dictates that the return values must be checked, despite the fact that failure means something was really wrong (like the hardware failing) and there's no way to recover from that, and you can argue that point as much as you like but your corporate-drone manager isn't going to care, only about whether you're "following the standards" and his precious metrics that are just as uncaring. (That's a rant for another day.)

On the other hand, no one argues whether you need to check for errors when using assignment statements. Doing it the old way is so much simpler and avoids the whole aforementioned argument issue around pointless error-checking.

Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by wildmusings on Saturday May 11, 2019 @08:28PM (#19889315)
You should always check the return value for an error, no matter what. I would not suggest otherwise. Whatever the source of the failure, you want to explicitly fail in a defined way rather than have your application behave in unexpected ways, potentially destroying user data or otherwise misbehaving.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by maxxxxx on Friday May 10, 2019 @10:32PM (#19884155)
Without a language that wraps the naked COM calls it's truly a nightmare. Using COM from C# is pretty OK and VB6 was great too. C++ is worse. To me COM is like an assembly language for interop that needs to be wrapped with higher language constructs. You could probably write a few C macros to reduce the boilerplate code a lot.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by snaky on Friday May 10, 2019 @11:14PM (#19884313)
ATL and WTL are such bunches of macros actually, but for C++.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by maxxxxx on Saturday May 11, 2019 @09:50AM (#19886133)
ATL yes, I think WTL is macros around Win32.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by dragontamer on Saturday May 11, 2019 @02:57PM (#19887785)
WTL is built on top of ATL. Both use C++ isms, like curiously recurring template pattern.

WTL is definitely "deep" C++. Templates, multiple inheritance, and more.

Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by maxxxxx on Saturday May 11, 2019 @05:16PM (#19888428)
I worked a little with WTL a while ago and it felt like MFC done right.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by pcwalton on Friday May 10, 2019 @10:11PM (#19884072)
Also, that code isn't C, it's C++. Method calls are a C++ feature. While you could use COM without language support for method calls, because the ABI is stable, it isn't really something that Microsoft supports.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by Iwan-Zotow on Friday May 10, 2019 @11:11PM (#19884298)
Of course Microsoft supports it, why not? There is no magic in COM, had COM object written in C
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by pcwalton on Friday May 10, 2019 @11:59PM (#19884461)
I don't mean that calling COM objects from C doesn't work. I just mean that COM isn't designed to be used from C.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by tonyedgecombe on Saturday May 11, 2019 @04:05AM (#19885144)
Actually I think it was, that was the lowest common denominator.
Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by pjmlp on Monday May 13, 2019 @06:13AM (#19897717)
Not really, COM is a subset of OLE 2.0, designed to replace VBX as OCX originally.

Naturally OLE was designed as C framework back in Win16 days, with endless pages of boilerplate code.

https://www.amazon.com/Windows-Programmers-Guide-Book-Disk/d...

As such COM can be called from C, specially since C++ Windows compilers have a VTBL layout as if they were a plain old C struct with function pointers.

However unless one is masochist, it is not sane to use COM from bare bones C.

Re:Microsoft Throws in the Towel on UWP, Elevates Win32 by teddyuk on Friday May 10, 2019 @10:28PM (#19884143)
I remember using C to call com components as a learning excercise - it was just that to see how it worked. You wouldn’t do it otherwise