Re: Serial no Help by oceanstorm
oceanstorm
Fri Nov 25 21:36:43 CST 2005
try it....! hope it help you...
oceanstorm
IBMS Co.,Ltd
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
namespace Devices
{
/// <summary>
/// Get summary description for DeviceInfo.
/// </summary>
sealed class DeviceInfo
{
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 DeviceInfo()
{
}
public 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 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[i]));
}
sb.Append("-");
for (int i = dwPlatformIDOffset; i < dwPlatformIDOffset + dwPlatformIDSize;
i ++ )
{
sb.Append( String.Format("{0:X2}", outbuff[i]));
}
return sb.ToString();
}
}
}
"Peter Foot [MVP]" <feedback@nospam-inthehand.com> wrote in message
news:e20njgI7FHA.3984@TK2MSFTNGP11.phx.gbl...
> The hardware ID returned by the KernelIOControl method will return a
unique
> id for the device, however most manufacturers assign a serial number which
> is printed externally on the unit which rarely matches the internal
hardware
> id.
>
> Peter
>
> --
> Peter Foot
> Windows Embedded MVP
> www.peterfoot.net | www.inthehand.com
>
> "Cush" <ryanjpcush@hotmail.com> wrote in message
> news:O4SPVAI7FHA.2264@TK2MSFTNGP11.phx.gbl...
> > Hi,
> > I have tried all the code examples in this forum for obtaining the
serial
> > number of my PDA(iPAQ 1710 running PPC2003) and they all work fine - but
> > none return the exact serial number thats on the back of the PDA. I
have
> > written code for a Casio DTX5 (running HandheldPC) and have used
> > CLBGetDeviceIDCode() which returns the exact code on the back of the
> > PDA - can the same be done in PPC because i want to tie each program I
> > write to each PDA so they cannot be copied.
> >
> > Thanks for any help on this
> >
> > Cush
> >
>
>