{"id":759,"date":"2012-06-26T00:07:56","date_gmt":"2012-06-25T14:07:56","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=759"},"modified":"2013-01-24T00:35:02","modified_gmt":"2013-01-23T13:35:02","slug":"read-computer-sid-64-bit-machine","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2012\/06\/26\/read-computer-sid-64-bit-machine\/","title":{"rendered":"How to read computer SID on 64-bit machine"},"content":{"rendered":"<div id=\"ntsbl-1527476795\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><p>To get a unique identifier for a computer the recommended key is &#8220;ProductId&#8221; that is accessible from the following registry key &#8211; HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion.<\/p>\n<p>This is the value you see as &#8220;Product ID&#8221; on the properties screen of your computer.<\/p>\n<p>To access this key can be a little problematic if you are attempting to read the &#8220;ProductId&#8221; sub-key on a 64-bit machine from a 32-bit application.<\/p>\n<p>This is due to Registry Reflection or WOW64 as it is sometimes called.<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa384235%28v=vs.85%29.aspx\">http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa384235%28v=vs.85%29.aspx<\/a><\/p>\n<p>In short the 64-bit OS shows 2 logical views of certain portions of the registry on WOW64, one for 32-bit and one for 64-bit. In the case of the &#8220;ProductId&#8221; key it explicitly exists in the 64-bit view so the following code will fail to find the key, as when running inside the 32-bit application it accesses the 32-bit view of the registry and fails to find the key.<\/p>\n<p>The following code will return an empty string<br \/>\n[csharp]<br \/>\npublic static string ComputerSID<br \/>\n{<br \/>\n    get<br \/>\n    {<br \/>\n        string sid = string.Empty;<\/p>\n<p>        string SIDKey = @&quot;SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion&quot;;<br \/>\n        RegistryKey key = Registry.LocalMachine.OpenSubKey(SIDKey);<\/p>\n<p>        object keyValue = key.GetValue(&quot;ProductId&quot;);<\/p>\n<p>        if (keyValue != null)<br \/>\n        {<br \/>\n            sid = keyValue.ToString();<br \/>\n        }<\/p>\n<p>        key = null;<br \/>\n        keyValue = null;<\/p>\n<p>        return sid;<br \/>\n    }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>To make this work you have to explicitly request c# to open the 64-bit view of the registry.<br \/>\nThe following code sample will work.<\/p>\n<p>[csharp]<br \/>\npublic static string ComputerSID<br \/>\n{<br \/>\n    get<br \/>\n    {<br \/>\n        string sid = string.Empty;<br \/>\n        string SIDKey = @&quot;SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion&quot;;<\/p>\n<p>        RegistryKey baseKey = null;<br \/>\n        if (Environment.Is64BitOperatingSystem)<br \/>\n        {<br \/>\n            baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);<br \/>\n        }<\/p>\n<p>        RegistryKey key = baseKey.OpenSubKey(SIDKey);<\/p>\n<p>        object keyValue = key.GetValue(&quot;ProductId&quot;);<\/p>\n<p>        if (keyValue != null)<br \/>\n        {<br \/>\n            sid = keyValue.ToString();<br \/>\n        }<\/p>\n<p>        key = null;<br \/>\n        keyValue = null;<\/p>\n<p>        return sid;<br \/>\n    }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>Cheers<br \/>\nJohn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To get a unique identifier for a computer the recommended key is &#8220;ProductId&#8221; that is accessible from the following registry key &#8211; HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion. This [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,6],"tags":[],"class_list":["post-759","post","type-post","status-publish","format-standard","hentry","category-c","category-code"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=759"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/759\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}