{"id":97735,"date":"2018-01-08T07:00:00","date_gmt":"2018-01-08T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97735"},"modified":"2019-03-13T01:02:16","modified_gmt":"2019-03-13T08:02:16","slug":"20180108-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180108-00\/?p=97735","title":{"rendered":"How do I get the computer&#8217;s serial number? Consuming Windows Runtime classes in desktop apps, part 3: C++\/WinRT"},"content":{"rendered":"<p>Continuing our series on getting the computer&#8217;s serial number in desktop apps in various languages, next up is C++\/WinRT. <\/p>\n<p>From Visual Studio, create a new C++ Console Application that goes like this: <\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt; \/\/ Horrors! Mixing C and C++!\n\n#include \"winrt\/Windows.System.Profile.SystemManufacturers.h\"\n\nint __cdecl wmain(int, char**)\n{\n  winrt::init_apartment();\n  {\n    auto serialNumber = winrt::Windows::System::Profile::\n         SystemManufacturers::SmbiosInformation::SerialNumber();\n    wprintf(L\"Serial number = %ls\\n\", serialNumber.c_str());\n  }\n\n  \/\/ The last thread cleans up before uninitializing for good.\n  winrt::clear_factory_cache();\n  winrt::uninit_apartment();\n\n  return 0;\n}\n<\/pre>\n<p>Before building, prepare the project as follows: <\/p>\n<ul>\n<li>Right-click the Project in Visual Studio     and select <i>Manage NuGet packages<\/i>.     Click the <i>Browse<\/i> tab and search for     &#8220;cppwinrt&#8221;, then click <i>Install<\/i>. <\/li>\n<li>Right-click the Project in Visual Studio     and select <i>Properties<\/i>.     Configure the project as follows: <\/li>\n<ul>\n<li>Configuration Properties, C\/C++ Language C++ Language Standard: Set to <b>ISO C++17 Standard (\/std:c++17)<\/b>. <\/li>\n<li>Configuration Properties, Linker, Inputs, Additional Dependencies: add <code>windowsapp.lib<\/code>. <\/li>\n<\/ul>\n<\/ul>\n<p>Okay, now you can build and run the program. <\/p>\n<p>C++\/WinRT lets you consume Windows Runtime objects without requiring any nonstandard language extensions. It&#8217;s all standard C++17. <\/p>\n<p>So that&#8217;s native code. Next up is C#. <\/p>\n<p><b>Bonus chatter<\/b>: <\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">Raymond Chen posted an example of reading a computers serial number with C++\/WinRT: <a href=\"https:\/\/t.co\/OaQClIEFJD\">https:\/\/t.co\/OaQClIEFJD<\/a><\/p>\n<p>Since Im fond of brevity, heres a more concise example: <a href=\"https:\/\/t.co\/Hijht189OA\">https:\/\/t.co\/Hijht189OA<\/a><\/p>\n<p>I would also recommend the version of C++\/WinRT available in the Windows SDK.<\/p>\n<p>&mdash; Kenny Kerr (@kennykerr) <a href=\"https:\/\/twitter.com\/kennykerr\/status\/950398492869984256?ref_src=twsrc%5Etfw\">January 8, 2018<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Getting into the modern swing of things.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-97735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Getting into the modern swing of things.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=97735"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97735\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=97735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}