mohasad

hi all ,
is there any option or api to get the device Id .
thanks
sadiq



Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0


Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

Zero Dai - MSFT

Hi sadiq,

You can use GetDeviceUniqueID (New API in Windows Mobile 5.0).

For more information and also managed sample code, you can read Windows Mobile Team Blog,

Link: http://blogs.msdn.com/windowsmobile/archive/2006/01/09/510997.aspx

Thanks!

Zero Dai - MSFT






Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

mohasad

hi all ,
i am having a problem on Device ID . i know that the length of the Device Id is around 20 bytes .i have generated the device ID using the following code .


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


namespace SudokuFusion
{
public class DeviceID
{
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);

public DeviceID()
{

}

public string GetDeviceID()
{

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 wasn't 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); // DEVICE_ID.dwPresetIDOffset
Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8); // DEVICE_ID.dwPresetSize
Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc); // DEVICE_ID.dwPlatformIDOffset
Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10); // DEVICE_ID.dwPlatformIDBytes
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();

}


}
}

The output is as follows :
53006D00610072007400500068006F006E0065000000-00

Is that the above ID is correct or is there any problem in code , If so give me a sample ID .
waiting for the reply

thanks
sadiq








Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

mohasad

hi Zerodai,
i am having a problem on Device ID . i know that the length of the Device Id is around 20 bytes .i have generated the device ID using the following code .


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


namespace SudokuFusion
{
public class DeviceID
{
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);

public DeviceID()
{

}

public string GetDeviceID()
{

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 wasn't 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); // DEVICE_ID.dwPresetIDOffset
Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8); // DEVICE_ID.dwPresetSize
Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc); // DEVICE_ID.dwPlatformIDOffset
Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10); // DEVICE_ID.dwPlatformIDBytes
StringBuilder sb = new StringBuilder();

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

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

}
return sb.ToString();

}


}
}

The output is as follows :
53006D00610072007400500068006F006E0065000000-00

Is that the above ID is correct or is there any problem in code , If so give me a sample ID .
waiting for the reply

thanks
sadiq






Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

Zero Dai - MSFT

Dear sadiq,

My output is, 50006F0063006B0065007400500043000000-00 (Windows Mobile 5.0 for Pocket PC Emulator)

Thanks!

Zero Dai - MSFT






Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

mohasad

hi zerodai,
is that the above code is correct . shall i use that code . one more doubt if we remove '-' does it work .
waiting for your reply

thanks
sadiq





Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

mohasad

hi ,
moreover the length of your code and mine differs. is the code that i pasted is right , shall i use that code for getting device ID . moreover if i remove ' - ' does it work .
thanks
sadiq






Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

Zero Dai - MSFT

Hi sadiq,

The code is right, so I can say that the device ID retrieved is also right.

Yes, we can remove '-', which is just a seperator for making the format clear. (You can look into the code.)

Actually, device ID consists of two IDs, Preset ID and PlatForm ID. Before '-' is Preset ID, after '-' is PlatForm ID.

Zero Dai - MSFT






Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

mohasad

hi zerodai,
from the above code , i come to know that we the one provide the application string . i.e Instead of IOCTL_HAL_GET_DEVICEID , shall we substitute someother string . any way its our decision to substitute the string that should be hashed with DeviceId .
am i right .waiting for your reply

thanks
sadiq







Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

Zero Dai - MSFT

Hi sadiq,

Yes, I agree with you. "The device ID is a resource that should be protected to mitigate the risk of identity theft", quoted from Windows Mobile Team Blog. This is also what GetDeviceUniqueID (new in Windows Mobile 5.0) brings to us.

So, for you application, you need to get the device ID using IOCTL_HAL_GET_DEVICEID first, and then use HashAlgorithm Class to get hashed value of the return device ID.

Regards,

Zero Dai - MSFT






Re: Smart Devices VB and C# Projects getting the device id of windowsmobile 5.0

mohasad

hi zerodai ,
when using this code to generate the device Id , almost the device Id seems to be same , only a small portion seems to vary
for e.g in Windows mobile emulator :
53006D00610072007400500068006F006E006500000000.

for Motorola Q emulator :

53006D00610072007400500068006F006E0065000000444556494345454D00


so is it going wrong ... moreover can we restrict the the device Id to a particular length i.e the length of the Id should be around 16 . so that it will be standard to process all the id ..waiting for ur reply

thanks
sadiq