How can i get Machine Id from Windows CE Device ?


In this post , I will explain how to get machine id in windows CE devices. To do that, we pass over to windows library that is coredll. The code below explains how to do that. Enjoy it !

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace DeviceSecurity
{
    public class DeviceSecurityTool
    {
        private static Int32 FILE_DEVICE_HAL = 0x00000101;
        private static Int32 FILE_ANY_ACCESS = 0x0;
        private static Int32 METHOD_BUFFERED = 0x0;
        private static Int32 IOCTL_HAL_GET_DEVICEID = ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) | ((21) << 2) | (METHOD_BUFFERED);

        [DllImport("coredll.dll")]
        private static extern bool KernelIoControl(
            Int32 IoControlCode,
            IntPtr InputBuffer,
            Int32 InputBufferSize,
            byte[] OutputBuffer,
            Int32 OutputBufferSize,
            ref Int32 BytesReturned);

        public extern IntPtr FindWindow(string ClassName, string WindowName);

        ///
        ///
        ///
        ///
        public  string GetHardwareKey()
        {
            try
            {
                byte[] OutputBuffer = new byte[256];
                Int32 OutputBufferSize, BytesReturned;
                OutputBufferSize = OutputBuffer.Length;
                BytesReturned = 0;

                bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, OutputBuffer, OutputBufferSize, ref BytesReturned);

                // If the request failed, exit the method now
                if (retVal == false)
                {
                    return null;
                }

                Int32 PresetIDOffset = BitConverter.ToInt32(OutputBuffer, 4);
                Int32 PlatformIDOffset = BitConverter.ToInt32(OutputBuffer, 0xc);
                Int32 PlatformIDSize = BitConverter.ToInt32(OutputBuffer, 0x10);

                StringBuilder sb = new StringBuilder();
                sb.Append(String.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
                     BitConverter.ToInt32(OutputBuffer, PresetIDOffset),
                     BitConverter.ToInt16(OutputBuffer, PresetIDOffset + 4),
                     BitConverter.ToInt16(OutputBuffer, PresetIDOffset + 6),
                     BitConverter.ToInt16(OutputBuffer, PresetIDOffset + 8)));

                for (int i = PlatformIDOffset; i < PlatformIDOffset + PlatformIDSize; i++)
                {
                    sb.Append(String.Format("{0:X2}", OutputBuffer[i]));
                }

                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }
    }
}
About these ads

3 thoughts on “How can i get Machine Id from Windows CE Device ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s