用C#开发PDA程序,PDA操作系统是PPC2003,请问在程序获取序列号?谢谢
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// 的PPC序列号
/// </summary>
private static Int32 _0x1F8CA = 0;
private static Int32 _0x1F81D = 0;
private static Int32 _0x39CF8 = 0x00000101;
private const Int32 _0xC0000000 = 0x32;
private const Int32 _0x00000231 = 0x7A;
private static Int32 _0x9C8F71F3 = ((_0x39CF8) << 16) | ((_0x1F81D) << 14) | ((21) << 2) | (_0x1F8CA);
[DllImport("CoreDll.dll", SetLastError=true)]
private static extern bool KernelIoControl(Int32 dwIoControlCode, IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, Int32 nOutBufSize, ref Int32 lpBytesReturned);
[DllImport("CoreDll.dll", SetLastError=true, CharSet=CharSet.Auto)]
private static extern bool KernelIoControl(int dwIoControlCode,
IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, IntPtr
lpBytesReturned);
public static string ReturnPPCSerial()
{
byte[] outbuff = new byte[20];
Int32 dwOutBytes;
bool done = false;
Int32 nBuffSize = outbuff.Length;
BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
dwOutBytes = 0;
while (! done)
{
if (KernelIoControl(_0x9C8F71F3, IntPtr.Zero, 0, outbuff, nBuffSize, ref dwOutBytes))
{
done = true;
}
else
{
int error = Marshal.GetLastWin32Error();
switch (error)
{
case _0xC0000000:
break;
case _0x00000231:
nBuffSize = BitConverter.ToInt32(outbuff, 0);
outbuff = new byte[nBuffSize];
BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
break;
default:
break;
}
}
}
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();
sb.Append("_");
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();
}