{"id":97725,"date":"2018-01-05T07:00:00","date_gmt":"2018-01-05T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97725"},"modified":"2019-03-13T01:02:13","modified_gmt":"2019-03-13T08:02:13","slug":"20180105-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180105-00\/?p=97725","title":{"rendered":"How do I get the computer&#8217;s serial number? Consuming Windows Runtime classes in desktop apps, part 2: C++\/CX"},"content":{"rendered":"<p><a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">Continuing<\/a> our series on getting the computer&#8217;s serial number in desktop apps in various languages, next up is C++\/CX. <\/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[Platform::STAThread]\nint __cdecl wmain(int, wchar_t**)\n{\n  <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">CCoInitialize<\/a> init;\n\n  auto serialNumber = Windows::System::Profile::SystemManufacturers::\n                       SmbiosInformation::SerialNumber;\n  wprintf(L\"Serial number = %ls\\n\", serialNumber-&gt;Data());\n\n  return 0;\n}\n<\/pre>\n<p>Before building, right-click the Project in Visual Studio and select <i>Properties<\/i>, and then make these changes: <\/p>\n<ul>\n<li>Configuration Properties, C\/C++, General, Additional <code>#using<\/code> Directories: Add these two directories, adjusting as appropriate for where you installed Visual Studio and the Windows SDK. <\/li>\n<ul>\n<li><code>C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcpackages<\/code> (so the compiler can find <code>platform.winmd<\/code>) <\/li>\n<li><code>C:\\Program Files (x86)\\Windows Kits\\10\\UnionMetadata\\10.0.16299.0<\/code> (so the compiler can find <code>windows.winmd<\/code>)&sup1; <\/li>\n<\/ul>\n<li>Configuration Properties, C\/C++, General, Consume Windows Runtime Extension: Set to <b>Yes (\/ZW)<\/b>. <\/li>\n<li>Configuration Properties, C\/C++, Code Generation, Enable Minimal Rebuild: Set to <b>No (\/Gm-)<\/b>. <\/li>\n<li>Configuration Properties, Linker, Inputs, Additional Dependencies: add <code>windowsapp.lib<\/code>. <\/li>\n<\/ul>\n<p>Okay, now you can build and run the program. <\/p>\n<p>Consuming Windows Runtime objects in C++\/CX is more convenient than accessing them raw, but it is a nonstandard Microsoft extension. <\/p>\n<p>You don&#8217;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. <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/cppcx\/casting-c-cx\">The Casting page on MSDN<\/a> explains how to convert between a hat-pointer and a regular pointer. <\/p>\n<p>Okay, so setting up the project was kind of ugly, but that&#8217;s okay, because things will get better before they get better. Up next is C++\/WinRT. <\/p>\n<p>&sup1; There are two copies of <code>windows.winmd<\/code>, a good one in the directory I gave above, and a bad one in the directory <code>Union&shy;Metadata\\<\/code><code>Facade<\/code>. If you use the bad one, you get an internal compiler error. <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/larryosterman\/\">Larry Osterman<\/a> tried to explain to me what the bad copy in Facade was for, but all I heard was the wah-wah of Charlie Brown&#8217;s teacher. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Annoying to set up, but easier to use.<\/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-97725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Annoying to set up, but easier to use.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97725","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=97725"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97725\/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=97725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}