How do I get the computer’s serial number? Consuming Windows Runtime classes in desktop apps, part 2: C++/CX

Continuing our series on getting the computer's serial number in desktop apps in various languages, next up is C++/CX.

From Visual Studio, create a new C++ Console Application that goes like this:

#include <windows.h>
#include <stdio.h> // Horrors! Mixing C and C++!

int __cdecl wmain(int, wchar_t**)
  CCoInitialize init;

  auto serialNumber = Windows::System::Profile::SystemManufacturers::
  wprintf(L"Serial number = %ls\n", serialNumber->Data());

  return 0;

Before building, right-click the Project in Visual Studio and select Properties, and then make these changes:

  • Configuration Properties, C/C++, General, Additional #using Directories: Add these two directories, adjusting as appropriate for where you installed Visual Studio and the Windows SDK.
    • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcpackages (so the compiler can find platform.winmd)
    • C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.16299.0 (so the compiler can find windows.winmd
  • Configuration Properties, C/C++, General, Consume Windows Runtime Extension: Set to Yes (/ZW).
  • Configuration Properties, C/C++, Code Generation, Enable Minimal Rebuild: Set to No (/Gm-).
  • Configuration Properties, Linker, Inputs, Additional Dependencies: add windowsapp.lib.

Okay, now you can build and run the program.

Consuming Windows Runtime objects in C++/CX is more convenient than accessing them raw, but it is a nonstandard Microsoft extension.

You don't have to build your entire application in C++/CX. You can write part of it in plain C++, and part of it in C++/CX, and the link the two pieces together. The Casting page on MSDN explains how to convert between a hat-pointer and a regular pointer.

Okay, so setting up the project was kind of ugly, but that's okay, because things will get better before they get better. Up next is C++/WinRT.

¹ There are two copies of windows.winmd, a good one in the directory I gave above, and a bad one in the directory Union­Metadata\Facade. If you use the bad one, you get an internal compiler error. Larry Osterman tried to explain to me what the bad copy in Facade was for, but all I heard was the wah-wah of Charlie Brown's teacher.

Comments (9)
  1. JAHA says:

    “because things will get better before they get better”…woah

    1. Peter Doubleday says:

      I think that’s actually “worse before they get better.” Of course, it depends upon the value system within which you judge worse and better.

      This seems to me (unsurprisingly, because it’s Interop) to be a considerably hairier implementation than yesterday’s. I really don’t like messing around with project settings unless I have to. Of course, it’s “less typing,” which is always nice for those of us whose knuckles are all worn out after thirty years of use, but it’s a style/pattern I would prefer to avoid, what with me being a maintenance programmer these days.

      An honest question here. If there’s an issue with mixing C/C++ headers and consequent behavior, why not just use cout/

      1. Rick C says:

        He must think it’s funny.

  2. mikeb says:

    “all I heard was the wah-wah of Charlie Brown’s teacher”

    TIL that there’s something Raymond doesn’t grok (even if it’s just because he doesn’t care enough to).

    1. pc says:

      Given that this blog is “for entertainment purposes only”, I’m betting that Raymond has a better idea of what Larry was trying to tell him than he lets on here. It’s probably that it’s complex, and while Raymond could work to understand it completely and then try to explain it to us, it would be a large enough aside that it’d detract too much from his main point of writing the series.

      It makes a better story this way, anyway.

      1. Peter Doubleday says:

        Must … resist … Fist of Nitpickers Corner.

        May I simply summarise my response to your comment as a “Yup++?”

  3. Ryan Phelps says:

    Did Larry retire? I miss his blog.

    1. skSdnW says:

      No. I asked him a year ago and he confirmed that his blog is not dead, he just has no time to post ( ). He is no longer on the audio team but I’m sure the things he is working on now would be interesting as well.

  4. Anon says:

    It’ll be interesting to see the C++/WinRT version. I’ve never written any WinRT code.

Comments are closed.

Skip to main content