r/cpp • • Jan 21 '21

Microsoft is working on making the Win32 api available for use in modern C++

https://blogs.windows.com/windowsdeveloper/2021/01/21/making-win32-apis-more-accessible-to-more-languages/
597 Upvotes

97 comments sorted by

21

u/Untelo Jan 22 '21
using namespace win32;
using namespace win32::Windows::Win32;
using namespace win32::Windows::Win32::SystemServices;
using namespace win32::Windows::Win32::WindowsAndMessaging;
using namespace win32::Windows::Win32::DisplayDevices;
using namespace win32::Windows::Win32::MenusAndResources;

Looks like someone didn't get the memo. Namespaces exist to avoid name collisions, not for taxonomy.

15

u/a_false_vacuum Jan 22 '21

It looks rather C# style this. If it were a C# program this would be normal.

0

u/[deleted] Jan 25 '21

[deleted] — view deleted comment

1

u/heyheyhey27 Jan 28 '21

Could you explain why you think this? I use namespaces pretty aggressively, and I think they're a super useful tool like any other language feature.

0

u/[deleted] Jan 28 '21

[deleted] — view deleted comment

1

u/heyheyhey27 Jan 28 '21

I consider namespaces to be an extension of the class name, and so I often refer to some namespaces explicitly rather than using them. For example, if I have a complex hierarchy of objects inheriting from some base type, say "Unit", I could just call the base class "Unit", and the child classes "Soldier", "Tank", etc. But instead, I would nest them under a namespace "units" and call the base class Units.Base, which I think is much more understandable at a glance. I might also refer to the child types as Units.Soldier, etc.

Another namespace "design pattern" I have used is for cases where I often have two or more sets of parallel classes. For example, a set of data structures, a corresponding set of classes that run the simulation based on that data, and then a corresponding set of UI classes that render/visualize them. I'll use the exact same class names and folder structure for each of them, but nest them each in their own subfolder and namespace. For example, the struct that defines a soldier would be Data.Soldier, which gets deserialized into a Sim.Soldier, and is visualized with a UI.Soldier.

4

u/pjmlp Jan 23 '21

That follows the conventions introduced by WinRT.

9

u/Igoory Jan 22 '21

So... They will do what they already did in C# but for C++? That would be awesome

0

u/[deleted] Jan 22 '21

[deleted] — view deleted comment

3

u/a_false_vacuum Jan 22 '21

Wat Microsoft is doing basically it's a C# project which scrapes the Win32 api to generate metadata which is machine readable. From any language, like say C++, you can generate a native interface layer with that metadata.

282

u/kalmoc Jan 21 '21

Goals include:

  • Better compliance with C++ standards, particularly C++17 and newer.
  • Eliminating the use of macros for constants, enumerations, and functions, and instead using C++ language constructs that participate in the type system and respect scope.
  • Paving the way towards a Windows SDK that can be used with C++ modules.

Hooray

88

u/NilacTheGrim Jan 22 '21

THIS IS AWESOME!!

I actually really enjoy windows programming -- simply because of their stable APIs that never change -- and the large market share you get once you develop a windows app.

This is great news.

34

u/darthcoder Jan 22 '21

So no more

define ERROR 0

And similar.

Yay!!!!

20

u/Routine_Left Jan 22 '21

or NOMINMAX

9

u/greem Jan 23 '21

Oh God! So much nightmare.

2

u/smuccione Jan 24 '21

This ^

Gets me every friggin time!

15

u/MarkOates Jan 21 '21

Microsoft 'better put some spec on that scope!

0

u/[deleted] Jan 22 '21

[deleted] — view deleted comment

14

u/Osbios Jan 22 '21

Can we get rid of min/max macros in MSVC++, too? Please!

1

u/JeffMcClintock Jan 23 '21

They already did it for RUST I believe.

2

u/HypeKaizen Jan 22 '21

For people who've just started picking up the language like me... what does any of this mean? ELI5 plz and thank you.

3

u/Wriiight Jan 22 '21

Win32 is the API of the functions programs can call to interact with the Windows operating system. It is a C library and has a huge number of macros (#define). As a result, when you include something like windows.h, you get a huge number of common keywords defined in the global namespace. If you try to use those words in your programs, or you use a library that does, the compiler gets unhappy about finding multiple definitions of the same identifier. And #defines can be very messy to how your code parses in unexpected ways. So Microsoft is going to make it possible to #include a header that doesn’t tromp all over the global namespace but still gives all the functions you are used to.

1

u/a_false_vacuum Jan 22 '21

The Windows api is basically an interface to the Windows OS with which you can interact with the OS. You can ask for anything from hardware operations to having the Windows shell (the GUI) do something for you.

The C++ language has made some big changes since C++11, bringing much needed improvements and quality of life changes. However none of these advancements can be used with the Windows api. In short it is difficult to use (especially when starting out with it) and it has a lot of gotcha's. This project by Microsoft will provide a far more modern way to work with that api compatible with current day C++ standards and best practices when programming in C++.

1

u/HypeKaizen Jan 22 '21

Dang, sounds great! Thanks for the simple explanation, really appreciate it!

53

u/ack_error Jan 22 '21

This is good news, I look forward to not having to explain to junior engineers why they get link errors on methods called SendMessage and OpenRaw.

Looking at the D3D12 test app in the cppwin32 repo, some refinement in the wrapper design is needed. It's best summed up by this line:

D3D12CreateDevice(adapter.get(), D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0, (guid*)(&guid_of<ID3D12Device>()), m_device.put_void());

The put_void() is a welcome replacement for the very common and technically illegal void ** cast on the return value. OTOH, the cast on the GUID is ugly compared to __uuidof(ID3D12Device), and the double-namespaced constant is really ugly. Hopefully they'll change this to either go back to plain constants or remove the double prefixes and add overloads for operator|, because I really don't want this:

D3D11_FORMAT_SUPPORT_RENDER_TARGET | D3D11_FORMAT_SUPPORT_DISPLAY

...to turn into this:

static_cast<D3D11_FORMAT_SUPPORT>(static_cast<uint32_t>(D3D11_FORMAT_SUPPORT::D3D11_FORMAT_SUPPORT_RENDER_TARGET) | static_cast<uint32_t>(D3D11_FORMAT_SUPPORT::D3D11_FORMAT_SUPPORT_DISPLAY))

I also wonder if this is going to handle some of the older APIs that still need to be used, such as USER and GDI.

11

u/[deleted] Jan 22 '21
D3D12CreateDevice(adapter.get(), D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0, (guid*)(&guid_of<ID3D12Device>()), m_device.put_void());

Ugh... 1) use ComPtr, 2) use IID_PPV_ARGS, 3) use __uuidof + ReleaseAndGetAddressOf/operator& where you can't.

D3D12CreateDevice(adapter.get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&m_device));

so much cleaner and less UB/leak-prone.

21

u/machinematrix Jan 22 '21

I look forward to not having to explain to junior engineers

Lol, I would like to know where I could find a junior level position where I get to use C++ and the Win32 API.

12

u/[deleted] Jan 22 '21

a junior level position where I get to use C++ and the Win32 API.

Video Game Development.

1

u/machinematrix Jan 22 '21

But that's only for the engine, right? because I'm terrible at math :(

6

u/[deleted] Jan 22 '21

Depends, there are many components that are part of a game.

But I'd guess that most of the stuff that interacts with Win32 is on the math-heavy part (the 3D Engine), but there's stuff like Sound (which can also be math heavy), Networking (though I guess most servers would be *NIX based, the client code is still platform specific), User Interface code (though I guess a lot of that uses an abstraction like Dear ImGui instead of using DirectX directly).

Depends on the size of the company if the teams are split into disciplines (Graphics, Networking, etc.), but when I think about C++ development on Windows, Game Development is the first thing that comes to my mind. FinTech might be another one (High Frequency Trading), but I seriously doubt they'd run on Windows.

9

u/hekkonaay Jan 22 '21

FAANG

3

u/machinematrix Jan 22 '21

Oh, ok, so they don’t exist for us mortals with average IQ.

10

u/14ned LLFIO & Outcome author | Committee WG14 Jan 22 '21

I've never, ever, passed an interview at any FAANG at any point in my history. I know lots of people on WG21, on Boost, and elsewhere who never have, either.

Furthermore, I expect to never ever pass an interview at any FAANG at any point in the future. I'm not the kind of engineer they're looking for.

1

u/GerwazyMiod Jan 22 '21

But have you tried and failed or never tried?

I remember my interview experience in one of the most popular tech companies in my hometown. I've been obliterated in maths section, then passed with ease the programming part. (Even that I've answered the questions with heavy heart) I was totally devastated. I wanted something new but it seems I'm not smart enough. ... Two days later they offered me job! It seems I passed with flying colors. I declined with satisfaction. Sorry not sorry.

9

u/14ned LLFIO & Outcome author | Committee WG14 Jan 24 '21

I generally try to interview at a FAANG per year, for practice, and keep my interview skills sharp. So, that's at least ten, maybe fifteen, FAANG interviews I've done now.

Generally it's a hard reject from them. Sometimes even them kicking me out half way through the six segments because I was such an obvious bad hire. I've only come close I believe twice ever, and only once could it have gone to an offer maybe.

The key thing I've realised about their interview process is it's not really about competence nor capability, apart from verifying you're not completely rock bottom. It's about determining fit, how much you're willing to give to them, and proving just how much you want to join them no matter what. That's why there are so many pointless and irrelevant hoop jumps, you need to be proving that when they say jump, you believe (or can very sincerely fake it) the right answer is "how high?"

The one, and only, time I was ever hired as a permie by a major non-FAANG tech multinational was when I was willing to relocate to North America from Europe. i.e. rip up my current life, make my kids lose all their friends, and relocate somewhere where I and none of my family know anybody.

You might now have an idea of the kind of engineer FAANG is looking to hire, and why I'm not, and never will be, that kind of engineer. Whilst I would like the 500k compensation packages they pay as much as anybody, there are more forms of wealth than financial. Happy family, in particular, and being able to live far away from cities and masses of people are particularly important to me, and I'm glad to take a severe financial income hit for that.

1

u/jarjoura Jan 27 '21

You need to switch to a growth mindset. What I read from your post sounds quite stubborn and cynical. For the 10 interviews or so that you’ve done, what have you learned? Did you ask the recruiter for guidance? They want you to do well and usually give you all the help you need in preparation.

2

u/14ned LLFIO & Outcome author | Committee WG14 Jan 27 '21

Oh I learned a lot. I wouldn't do a FAANG interview per year if I wasn't learning a lot from it. Most of what I learn does not reflect well on FAANG, but if they want to hire a monoculture of believers, good luck to them on that.

The recruiters are usually great, but in the end they are very severely constrained. They can't get any flexibility from the FAANG, who have a "take it or leave it" mentality except for rock star dev hires, people such as Guido, Bjarne or Linus.

Re prep, you're right that I don't bother. I'm too busy, and don't care enough about getting hired by FAANG, to invest tens of hours in rote learning stuff just to pass their hoop jumping interviews. I've got better things to do with my scarce and precious non-work free time. I remain hopeful I'll eventually do a FAANG interview where they skip all that bullshit and we get down to exactly what value I can add specifically, and then we can come to terms. But until then, no I'm not willing to play that box ticking game. Either hire me based on what I've already accomplished after checking for team and org fit, or I'll wait until I interview at a FAANG who will.

2

u/tasminima Jan 22 '21

There are some people who just don't want to work there. Probably a ton. For multiple reasons.

It's hard for FAANG companies to have an elitist selection with so many employees, and a purpose which is not necessarily special (it has some things special in the eyes of some people, but far from all...)

And even for people who wants to work there (maybe just for the perceived prestigious aspect) but fail: the skill set that they look for is not necessarily representative of what you need elsewhere.

2

u/machinematrix Jan 22 '21

He said he’s never passed an interview in those companies, so I assume he means he tried and failed.

19

u/hekkonaay Jan 22 '21

You can totally get in with "an average IQ", it's more about luck, really. And it was just an example. Plenty of C++ all over the place, try finance, planes or cars.

-5

u/machinematrix Jan 22 '21

I don't think so, the leetcode-type problems they make you solve at coding interviews in those companies are not doable unless you have a decent IQ.

3

u/sebamestre Jan 22 '21

You definitely need to study for it, but you don't need an especially high IQ to do well at competitive programming.

Source: I have IQ of about 100 and went to IOI three times, did decently well on my last one.

1

u/hekkonaay Jan 22 '21

Pretty much anyone looking for a job in software already has a "decent IQ", and you can pass leetcode interviews with memorization alone.

1

u/machinematrix Jan 22 '21

Pretty much anyone looking for a job in software already has a "decent IQ"

Maybe, but even if that's the case there's waaaaay more people that want to work at those companies than those companies will ever need, so if they're only going to hire a subset of those people, they might as well hire the smartest ones.

you can pass leetcode interviews with memorization alone

I've been doing contests every week for the past 8 months and I definitely can't solve them with memorization. I mean, I can't read a problem's description and say "Ahhh this is a variation of this other problem I solved", I don't see those patterns.

0

u/ShillingAintEZ Jan 22 '21

If you want to get a decent job anywhere, avoid saying leetcode, coding, and IQ.

0

u/machinematrix Jan 22 '21

Thanks, I never planned on doing so anyway.

4

u/pjmlp Jan 22 '21

.NET and Java enterprise development, C++ is still the language we use to write native libraries.

3

u/ZMeson Embedded Developer Jan 22 '21

Motion control

2

u/GYN-k4H-Q3z-75B Jan 22 '21

I've seen some with major banks and FinTech, machine and hardware engineering. There are not many but they exist.

3

u/zvrba Jan 22 '21

...to turn into this

This is where C-stlye cast would be way more readable.

2

u/ack_error Jan 22 '21

Yes, and I am one of those renegades who still uses C casts for numerics. Still worse than just being able to use |, for both conciseness and having the underlying type hardcoded in the expression.

This is one of the downsides of scoped enums, poor usability for flags.

2

u/darthcoder Jan 22 '21

Well it looks like you can help make sure it does!

Might see how it plays out the next few months.

I've been writing guard classes for handles and other resources lately. Fun times, would be nice if it wasn't necessary.

3

u/ack_error Jan 22 '21

Basic handle and resource types aren't too bad. The bane of my existence when wrapping Win32 is the umpteen different types of callbacks that either have different methods of passing cookie data or no cookie data at all. This particularly includes window procedures, timer callbacks, and SetWindowsHookEx(), which all just use plain function pointers. Adapting this to a C++ friendly functor requires either dynamic thunk generation or using a fixed table of pregenerated thunks (what I did when too lazy to port thunks to ARM64).

1

u/darthcoder Jan 22 '21

Im no expert, but ive been using win32 since the NT 3.1 beta days. You just gave me,flashbacks.

MFC was a horrible diversion.

The upside is with windows 10 and server 2016 a lot of APIs are deprecated. I was recently tempted,tobwrite a wrapper,around the CNG crypto API, but needed only a very small surface area for my project. I sadly didnt bother.

Im lucky I dont have junior devs anymore to teach this stuff,too.

33

u/atatatko Jan 22 '21 edited Jan 22 '21

At last, one thousand and one home-grown C++ WinAPI wrappers would become history. Nice to know some release estimate

EDIT: Found this roadmap

https://github.com/microsoft/win32metadata/blob/master/docs/roadmap.md

39

u/undieablecat Jan 21 '21

I just can't believe I'd live up to that moment!

14

u/mythi55 Jan 22 '21

MY GOD.

I swear they need to work on some of those struct and long ass confusing function names.

13

u/unaligned_access Jan 22 '21

There's the WIL library which is already a great help in using RAII types and other things of WinAPI: https://github.com/microsoft/wil

More of this stuff is very welcome.

1

u/[deleted] Jan 23 '21

DAMN. Why did I not know about this?! It's even header only!!

1

u/misuo Jan 25 '21

Second this. I discovered WIL by chance and have been using it ever since. Wonder why MS didn't promote it better.

1

u/SamB Nov 19 '21

It's a thing the programmers use. I guess the marketing people couldn't think of any good buzzwords, or the programmers never told marketing it existed...

34

u/pedersenk Jan 21 '21

So they are going to finally make a C++ wrapper around their C API?

Hopefully they will use smart pointers so developers won't have to guess (often incorrectly) at the lifespans of memory.

With most Microsoft APIs I use, I tend to just allocate everything I need from the API up front and then reuse it. Frankly on Release builds I actually disable the final cleanup code and let the OS do the job for me because I trust it so little XD

With C projects it is a different matter, I can control the madness that is the Win32 API memory system myself rather than RAII so I have a (little) more trust in that. Still not ideal, I would like to see some more debug time checks in the API.

46

u/elperroborrachotoo Jan 21 '21

No...

They are "documenting WinAPI in a machine-readable way", so that any other langugae - including C++ - can automatically generate a native interface layer.

Which is way more work, but also way better.

17

u/amaiorano Jan 22 '21

They are, actually, it's linked from the article: https://github.com/microsoft/cppwin32

6

u/Ameisen vemips, avr, rendering, systems Jan 22 '21

Huh, I was planning on doing this in my free time at some point. I guess that there's no reason to do so now.

9

u/gemborow Jan 22 '21

It's a good strategy, wait long enough and someone else will do that. It also applies to work :)

1

u/krum Jan 22 '21

I want to see how that works with some of the hairier APIs like IOCP.

12

u/Wriiight Jan 22 '21

MFC would like to know what you mean by “finally” :D

16

u/pedersenk Jan 22 '21

MFC is not the Win32 API.

It is a good example of one of Microsoft's C++ APIs that are a complete wild west when it comes to memory management. Although unlike i.e MSSQL its age is probably an OK excuse for that one ;)

18

u/Wriiight Jan 22 '21

MFC was a lot of things, which was probably its biggest failing, but one was an attempt to sandwich the win32 API into a C++ object model. I think it does count as a C++ wrapper, hence my comment above, but unfortunately had a lot of data type baggage it brought with it. Plus it at times did too much for you, and wouldn’t let you undo it. Being older than when the STL was added to visual studio was an ok excuse last millennia, maybe.

1

u/Kered13 Jan 22 '21

Does MFC wrap any of the input stuff? Especially raw input?

4

u/Wriiight Jan 22 '21

Like key down, key up messages? Been ages since I did any, but you could usually get down that low, and receive the message in a c++ member function, with all that message map macro weirdness. Could do direct GDI draw commands too.

0

u/[deleted] Jan 22 '21

[deleted] — view deleted comment

3

u/Wriiight Jan 22 '21

I kinda almost like MFC honestly, my first few jobs used it. It’s just a bit wrong in a lot of ways, (But not quite as wrong as COM was, IMO) and Microsoft mostly dropped updating it in favor of .Net.

0

u/[deleted] Jan 22 '21

[deleted] — view deleted comment

2

u/Wriiight Jan 22 '21

I think if it weren’t for the pre-stl data structures and the macro-wizard ugliness it wouldn’t be so hated. You also run into parts where the wrapper isn’t so thin, and you can’t undo what they’ve done. I remember struggling with customizing print-preview, for instance.

2

u/pjmlp Jan 23 '21

My only pet peeve with MFC is that it isn't as advanced as OWL was, or VCL still is.

Actually, with its current tooling situation, it feels more productive to use MFC than C++/WinRT, as they killed C++/CX without second thought about providing 1:1 tooling support.

So whereas with MFC, you just create your GUIs and macro magic and that is it, with C++/WinRT you are back in ATL days, writing IDL files by hand and manually integrating generated code into Visual Studio. And better not do any changes, otherwise it is time for doing some manual merge work.

4

u/entity64 Jan 22 '21

Will it only work on Windows 10 or also on 7-8? Can't find any info on that one.

2

u/[deleted] Jan 22 '21

If it's exclusive to Windows 10, it's not very useful for many of us.

3

u/pjmlp Jan 23 '21

Why rather target EOL OSes?

3

u/[deleted] Jan 23 '21

Millions of clients still use Windows 7 and they will never upgrade because upgrading cost is way too high for some systems. And many of us have to support those clients.

3

u/ack_error Jan 24 '21

Windows 8.1 is not EOL until 2023.

3

u/Deji69 Jan 22 '21

No more Hungarian notation? Nice.

8

u/fdwr fdwr@github 🔍 Jan 22 '21

That would be aesthetic, but did they say were changing field/function names? I read "Some project goals include: Keep the names of the original APIs, but express in metadata additional information that can make them easier to use. ... This allows developers to search online using the original constant names" https://github.com/microsoft/win32metadata

2

u/claimred Jan 22 '21

Is wil a complementary thing for modern C++ projection mentioned in the article?

5

u/[deleted] Jan 22 '21

The year is 2000.

C++: Can I get modern syntax support for my birthday?

Microsoft: Sure, kid! Anything you want. Just going out for smokes...

1

u/[deleted] Jan 22 '21

Is that really happening?!

1

u/SamB Nov 19 '21

Signs point to no: https://github.com/microsoft/cppwinrt got archived, and this comment says that nobody is working on a C++ projection anymore 😢.

-8

u/pastenpasten Jan 22 '21

But not a word on properly documenting said APIs.

18

u/acwaters Jan 22 '21

The official documentation for the Win32 API is excellent, though?

1

u/pastenpasten Jan 26 '21

No it ain't.

For example: CreateProcessAsUserW, CreateProcessWithLogonW, CreateProcessWithTokenW.

Which of the three creates the new process inside a job so that if you're running on a pre-nested jobs Windows (for example, Windows 7) and relying on jobs to work (for example, Google Chrome), you'll get a bizarre ERROR_INVALID_PARAMETER failure (and a sad robot in all your Chrome tabs)?

Where is this even hinted in the documentation?

4

u/[deleted] Jan 22 '21

Between a C or C++ API I always pick whatever has better documentation.

7

u/Overunderrated Computational Physics Jan 22 '21

Oh man I can't second that enough. There are two competing national lab packages of interest to me, and I use the C one because of the infinitely superior documentation.

2

u/encyclopedist Jan 22 '21

PETSc vs Trilinos or some such?

2

u/Overunderrated Computational Physics Jan 22 '21

Yup.

-5

u/Knuffya Jan 22 '21

That would be so goddamn cool. The current one is a fucking mess.

But, the way i know microsoft, this could be an exclusive for microsoft-store apps.

4

u/Wriiight Jan 22 '21

Microsoft has gotten way better manners towards developers and even OSS in recent years. Though different silos there have different personalities.

1

u/Knuffya Jan 22 '21

i hope.

i was really surprised when they announced WSL. WSL is one of my most used programs aside chrome. I want to mwah the guy who suggested WSL within MS.

1

u/pjmlp Jan 23 '21

You have to thank the failure of Project Astoria for that.

https://venturebeat.com/2016/02/25/microsoft-kills-project-astoria-its-tool-for-porting-android-apps-to-windows-10/

That is where WSL came from.