Get UUID of a windows PC

Windows
public enum Provider : int
    {
        ACPI = (byte)'A' << 24 | (byte)'C' << 16 | (byte)'P' << 8 | (byte)'I',
        FIRM = (byte)'F' << 24 | (byte)'I' << 16 | (byte)'R' << 8 | (byte)'M',
        RSMB = (byte)'R' << 24 | (byte)'S' << 16 | (byte)'M' << 8 | (byte)'B'
    }

    static class NativeMethods
    {
        private const string KERNEL = "kernel32.dll";

        [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
          SetLastError = true)]
        public static extern int EnumSystemFirmwareTables(
          Provider firmwareTableProviderSignature,
          IntPtr firmwareTableBuffer, int bufferSize);

        [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
          SetLastError = true)]
        public static extern int GetSystemFirmwareTable(
          Provider firmwareTableProviderSignature,
          int firmwareTableID, byte[] firmwareTableBuffer, int bufferSize);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    class RawSMBIOSData
    {
        public byte Used20CallingMethod;
        public byte SMBIOSMajorVersion;
        public byte SMBIOSMinorVersion;
        public byte DmiRevision;
        public int Length;
        public IntPtr SMBIOSTableData;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
    class Dmi_Header
    {
        public byte type;
        public byte length;
        public ushort handle; //WORD
    };
    

    internal class Program
    {
        static string dmi_system_uuid(byte[] p, short ver)
        {

            if (ver >= 0x0206)
            {
                return $"{p[3]:X2}{p[2]:X2}{p[1]:X2}{p[0]:X2}-{p[5]:X2}{p[4]:X2}-{p[7]:X2}{p[6]:X2}-{p[8]:X2}{p[9]:X2}-{p[10]:X2}{p[11]:X2}{p[12]:X2}{p[13]:X2}{p[14]:X2}{p[15]:X2}";
            }
            else
            {
                return $"-{p[0]:X2}{p[1]:X2}{p[2]:X2}{p[3]:X2}-{p[4]:X2}{p[4]:X2}-{p[6]:X2}{p[7]:X2}-{p[8]:X2}{p[9]:X2}-{p[10]:X2}{p[11]:X2}{p[12]:X2}{p[13]:X2}{p[14]:X2}{p[15]:X2}";
            }
        }

        static void Main(string[] args)
        {
            byte[] buff = null;

            int buffSize = NativeMethods.GetSystemFirmwareTable(Provider.RSMB, 0, buff, 0);

            if (buffSize > 0)
            {
                buff = new byte[buffSize];

                buffSize = NativeMethods.GetSystemFirmwareTable(Provider.RSMB, 0, buff, buffSize);

                RawSMBIOSData rawSMBIOSData = new RawSMBIOSData();

                IntPtr pBuf = Marshal.AllocHGlobal(buffSize);
                Marshal.Copy(buff, 0, pBuf, buffSize);
                
                Marshal.PtrToStructure(pBuf, rawSMBIOSData);

                int offset = 0;
                IntPtr pTable = Marshal.AllocHGlobal(rawSMBIOSData.Length);
                Marshal.Copy(buff, buffSize - rawSMBIOSData.Length, pTable, rawSMBIOSData.Length);

                for (int i = 0; i < rawSMBIOSData.Length; i++)
                {

                    Dmi_Header dmiHeader = new Dmi_Header();
                    Marshal.PtrToStructure(pTable, dmiHeader);

                    if (dmiHeader.type != 1)
                    {
                        offset += dmiHeader.length;
                        pTable = new IntPtr(pTable.ToInt64() + offset);

                        while (Marshal.ReadInt16(pTable) != 0)
                        {
                            pTable = IntPtr.Add(pTable, 1);
                        }

                        pTable = IntPtr.Add(pTable, 2);

                        continue;
                    }

                    byte[] uuid = new byte[16];
                    Marshal.Copy(IntPtr.Add(pTable, 8), uuid, 0, uuid.Length);


                    string uuidString = dmi_system_uuid(uuid, (short)(rawSMBIOSData.SMBIOSMajorVersion * 0x100 + rawSMBIOSData.SMBIOSMinorVersion));

                    Console.WriteLine(uuidString);
                    
                }

            }

        }
    }

Same result is acquired by running: wmic csproduct get UUID

Post a Comment

Previous Post Next Post