locked
get device unique id RRS feed

  • Question

  • Hi to all,

    Im working in CF1.0. How can i get the device unique id?.

    Currently i am importing the coredll.dll and using GetDeviceUniqueID(...) method
    to get the device unique id in a byte array. But this method not supported by the smartphone & pocketpc 2003.
    Is there any other way to get device id to all the devices like smartdevices 2003, WM 5.0 and WM 6. Plz help me
    resolve this problem..


    thanks
    vaidhees.
    Thursday, July 31, 2008 1:28 PM

Answers

All replies

  • Hi,

     

    Take a look at this forum posting :-

     

    http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3688058&SiteID=1

     

    Hope this helps.

     

    Paul Diston

     

    Thursday, July 31, 2008 5:00 PM
  • Hi vaidhees,

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Collections;
    using System.Diagnostics;

    namespace device_ID_5._pp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private static Int32 METHOD_BUFFERED = 0;
            private static Int32 FILE_ANY_ACCESS = 0;
            private static Int32 FILE_DEVICE_HAL = 0x00000101;

            private const Int32 ERROR_NOT_SUPPORTED = 0x32;
            private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;

            private static Int32 IOCTL_HAL_GET_DEVICEID =
                ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
                | ((21) << 2) | (METHOD_BUFFERED);

            [DllImport("coredll.dll", SetLastError = true)]
            private static extern bool KernelIoControl(Int32 dwIoControlCode,
                IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf,
                Int32 nOutBufSize, ref Int32 lpBytesReturned);

            private static string GetDeviceID()
            {
                // Initialize the output buffer to the size of a
                // Win32 DEVICE_ID structure.
                byte[] outbuff = new byte[20];
                Int32 dwOutBytes;
                bool done = false;

                Int32 nBuffSize = outbuff.Length;

                // Set DEVICEID.dwSize to size of buffer.  Some platforms look at
                // this field rather than the nOutBufSize param of KernelIoControl
                // when determining if the buffer is large enough.
                BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
                dwOutBytes = 0;

                // Loop until the device ID is retrieved or an error occurs.
                while (!done)
                {
                    if (KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero,
                        0, outbuff, nBuffSize, ref dwOutBytes))
                    {
                        done = true;
                    }
                    else
                    {
                        int error = Marshal.GetLastWin32Error();
                        switch (error)
                        {
                            case ERROR_NOT_SUPPORTED:
                                throw new NotSupportedException(
                                    "IOCTL_HAL_GET_DEVICEID is not supported on this device",
                                    new Win32Exception(error));

                            case ERROR_INSUFFICIENT_BUFFER:

                                // The buffer is not big enough for the data.  The
                                // required size is in the first 4 bytes of the output
                                // buffer (DEVICE_ID.dwSize).
                                nBuffSize = BitConverter.ToInt32(outbuff, 0);
                                outbuff = new byte[nBuffSize];

                                // Set DEVICEID.dwSize to size of buffer.  Some
                                // platforms look at this field rather than the
                                // nOutBufSize param of KernelIoControl when
                                // determining if the buffer is large enough.
                                BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
                                break;

                            default:
                                throw new Win32Exception(error, "Unexpected error");
                        }
                    }
                }
                Int32 dwPresetIDOffset = BitConverter.ToInt32(outbuff, 0x4);
                Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8);
                Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc);
                Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10);
                StringBuilder sb = new StringBuilder();

                for (int i = dwPresetIDOffset;
                    i < dwPresetIDOffset + dwPresetIDSize; i++)
                {
                    sb.Append(String.Format("{0:X2}", outbuffIdea));
                }

                sb.Append("-");

                for (int i = dwPlatformIDOffset;
                    i < dwPlatformIDOffset + dwPlatformIDSize; i++)
                {
                    sb.Append(String.Format("{0:X2}", outbuffIdea));
                }
                return sb.ToString();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    // Show the device ID.
                    string strDeviceID = GetDeviceID();
                    MessageBox.Show("Device ID: " + strDeviceID);

                    // Show the device name.
                    string deviceName = System.Net.Dns.GetHostName();
                    MessageBox.Show("Device Name: " + deviceName);
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
    }

    thanks
    Pravej alam
    Friday, August 1, 2008 4:42 AM