{"id":97715,"date":"2018-01-04T07:00:00","date_gmt":"2018-01-04T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97715"},"modified":"2019-03-13T01:02:06","modified_gmt":"2019-03-13T08:02:06","slug":"20180104-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180104-00\/?p=97715","title":{"rendered":"How do I get the computer&#8217;s serial number? Consuming Windows Runtime classes in desktop apps, part 1: Raw C++"},"content":{"rendered":"<p>Getting the computer&#8217;s serial number used to be an arduous task of <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms724379(v=vs.85).aspx\">getting the system firmware table<\/a>, and then manually parsing the SMBIOS information looking for the serial number. <\/p>\n<p>Windows 8 introduced the <code>Windows.<\/code><code>System.<\/code><code>Profile.<\/code><code>System&shy;Manufacturers.<\/code><code>SmbiosInformation<\/code> runtime class which parses out the serial number for you. <\/p>\n<p><a HREF=\"http:\/\/tvtropes.org\/pmwiki\/pmwiki.php\/Main\/TheEasyWayOrTheHardWay\">We can do this the easy way or the hard way<\/a>. Let&#8217;s do it the hard way. <\/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;wrl\/client.h&gt;\n#include &lt;wrl\/wrappers\/corewrappers.h&gt;\n#include &lt;windows.system.profile.systemmanufacturers.h&gt;\n#include &lt;roapi.h&gt;\n#include &lt;stdio.h&gt; \/\/ Horrors! Mixing C and C++!\n\nnamespace WRL = Microsoft::WRL;\nnamespace spsm = ABI::Windows::System::Profile::SystemManufacturers;\n\nusing Microsoft::WRL::Wrappers::HString;\nusing Microsoft::WRL::Wrappers::HStringReference;\n\nint __cdecl wmain(int, wchar_t**)\n{\n    <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">CCoInitialize<\/a> init;\n\n    WRL::ComPtr&lt;spsm::ISmbiosInformationStatics&gt; statics;\n    HString serialNumber;\n    RoGetActivationFactory(HStringReference(\n        RuntimeClass_Windows_System_Profile_SystemManufacturers_SmbiosInformation)\n                             .Get(), IID_PPV_ARGS(&amp;statics));\n    statics-&gt;get_SerialNumber(serialNumber.GetAddressOf());\n    wprintf(L\"Serial number = %ls\\n\", serialNumber.GetRawBuffer(nullptr));\n\n    return 0;\n}\n<\/pre>\n<p>Before building, right-click the Project in Visual Studio and select <i>Properties<\/i>. From the dialog, make the following change: <\/p>\n<ul>\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, and it&#8217;ll tell you your system&#8217;s serial number as recorded in the SMBIOS. <\/p>\n<p>And fortunately you didn&#8217;t have to go parsing the system firmware table yourself. <\/p>\n<p>This series repeats the above exercise in desktop apps in various languages, each one getting easier than the previous one. Next up is C++\/CX. <\/p>\n<p>(The secret goal of this series is to capture how you need to configure your project to get it to build.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can do this the easy way or the hard way. Let&#8217;s do it the hard way.<\/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-97715","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We can do this the easy way or the hard way. Let&#8217;s do it the hard way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97715","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=97715"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97715\/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=97715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}