Win32_PhysicalMedia returns a different serial number for non-admin users

I used the following query to get the serial number of the hard drive.

ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); 

It returns a different serial number for admin and non-admin as follows:

admin - WD-WCAYUS426947 non-admin - 2020202057202d44435759415355323439363734

When you tried to put the serial number without an administrator in the hexadecimal code in the char converter, it gave W -DCWYASU249674 , which is actually a character replacement for every 2 characters.

Any idea to get the correct series without manupulating un-hexed format please?

+6
source share
2 answers

As posted in the comments: This seems like an unresolved error on Windows, although Microsoft knows about it .

The way to solve it is to convert the hexadecimal string and replace the numbers, I wrote a method that does this for you, do not hesitate to edit it according to your needs:

  public static string ConvertAndSwapHex(string hex) { hex = hex.Replace("-", ""); byte[] raw = new byte[hex.Length / 2]; for (int i = 0; i < raw.Length; i++) { int j = i; if (j != 0) { j = (j % 2 == 1 ? j-1 : j+1); } raw[j] = Convert.ToByte(hex.Substring(i * 2, 2), 16); } return System.Text.Encoding.UTF8.GetString(raw).Trim(' ', '\t', '\0'); } 
+5
source

Many thanks to @thedutchman for writing down the code for converting and replacing adjacent characters. I combined your code and the code in this link and created a new function, as shown below:

  public static string ConvertAndSwapHex(string hexString) { var charString = new StringBuilder(); for (var i = 0; i < hexString.Length; i += 4) { charString.Append(Convert.ToChar(Convert.ToUInt32(hexString.Substring(i + 2, 2), 16))); charString.Append(Convert.ToChar(Convert.ToUInt32(hexString.Substring(i, 2), 16))); } return charString.ToString(); } 

Another important thing, as mentioned in this forum for Microsoft , using Win32_DiskDrive instead of Win32_PhysicalMedia sequentially returns the zinxed serial number in Win 7 for admin and non-admin users. Despite the fact that it returns completely different things to WinXP (which we no longer support for our software), sequentially returning the smoothing serial number is good enough for me. You know that I donโ€™t need to check the length of the serial number to determine if I need to use the above ConvertAndSwap method or not.

+1
source

Source: https://habr.com/ru/post/969984/


All Articles