分享

c# wince 取设备号

 偷心无痕 2014-08-27

wince设备,有时为了做软件保护,采取注册限制,需要取设备的序列号,以下为通用的取法,网上抄的。

  1. using   System;   
  2. using   System.Collections.Generic;   
  3. using   System.ComponentModel;   
  4. using   System.Data;   
  5. using   System.Drawing;   
  6. using   System.Text;   
  7. using   System.Windows.Forms;   
  8. using   System.Collections;   
  9. using   System.Diagnostics;   
  10. using   System.Runtime.InteropServices;   
  11. using   System.IO;   
  12. using   System.Security.Cryptography;  
  13.   
  14. namespace Check  
  15. {  
  16.     class LiangDeviceID  
  17.     {  
  18.         private static string[] strEncrypt = new string[] { "1""2""3""4""5""6""7""8""9""0""K""L""M""N""O""P""Q""R""S""T""U""V""W""X""Y""Z""AA""AB""AC""AD""AE""AF""AG""AH""AI""AJ""AK""AL""AM""AN""AO""AP" };  
  19.         private   static   Int32   METHOD_BUFFERED   =   0;   
  20.         private   static   Int32   FILE_ANY_ACCESS   =   0;   
  21.         private   static   Int32   FILE_DEVICE_HAL   =   0x00000101;   
  22.   
  23.         private   const   Int32   ERROR_NOT_SUPPORTED   =   0x32;   
  24.         private   const   Int32   ERROR_INSUFFICIENT_BUFFER   =   0x7A;   
  25.   
  26.   
  27.         private   static   Int32   IOCTL_HAL_GET_DEVICEID   =   ((FILE_DEVICE_HAL)   <<   16)   |   ((FILE_ANY_ACCESS)   <<   14)   |   ((21)   <<   2)   |   (METHOD_BUFFERED);   
  28.   
  29.   
  30.         [DllImport( "coredll.dll",   SetLastError   =   true)]   
  31.         private   static   extern   bool   KernelIoControl(Int32   dwIoControlCode,   IntPtr   lpInBuf,   Int32   nInBufSize,   byte[]   lpOutBuf,   Int32   nOutBufSize,   ref   Int32   lpBytesReturned);   
  32.   
  33.         public   static   string   GetDeviceID()   
  34.         {   
  35.             try  
  36.             {  
  37.                 //   Initialize   the   output   buffer   to   the   size   of   a   Win32   DEVICE_ID   structure   
  38.                 byte[] outbuff = new byte[20];  
  39.                 Int32 dwOutBytes;  
  40.                 bool done = false;  
  41.   
  42.                 Int32 nBuffSize = outbuff.Length;  
  43.   
  44.                 //   Set   DEVICEID.dwSize   to   size   of   buffer.     Some   platforms   look   at   
  45.                 //   this   field   rather   than   the   nOutBufSize   param   of   KernelIoControl   
  46.                 //   when   determining   if   the   buffer   is   large   enough.   
  47.                 //   
  48.                 BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);  
  49.                 dwOutBytes = 0;  
  50.   
  51.   
  52.                 //   Loop   until   the   device   ID   is   retrieved   or   an   error   occurs   
  53.                 while (!done)  
  54.                 {  
  55.                     if (KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, outbuff, nBuffSize, ref   dwOutBytes))  
  56.                     {  
  57.                         done = true;  
  58.                     }  
  59.                     else  
  60.                     {  
  61.                         int error = Marshal.GetLastWin32Error();  
  62.                         switch (error)  
  63.                         {  
  64.                             case ERROR_NOT_SUPPORTED:  
  65.                                 throw new NotSupportedException("IOCTL_HAL_GET_DEVICEID   is   not   supported   on   this   device "new Win32Exception(error));  
  66.   
  67.                             case ERROR_INSUFFICIENT_BUFFER:  
  68.                                 //   The   buffer   wasn 't   big   enough   for   the   data.     The   
  69.                                 //   required   size   is   in   the   first   4   bytes   of   the   output   
  70.                                 //   buffer   (DEVICE_ID.dwSize).   
  71.                                 nBuffSize = BitConverter.ToInt32(outbuff, 0);  
  72.                                 outbuff = new byte[nBuffSize];  
  73.   
  74.                                 //   Set   DEVICEID.dwSize   to   size   of   buffer.     Some   
  75.                                 //   platforms   look   at   this   field   rather   than   the   
  76.                                 //   nOutBufSize   param   of   KernelIoControl   when   
  77.                                 //   determining   if   the   buffer   is   large   enough.   
  78.                                 //   
  79.                                 BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);  
  80.                                 break;  
  81.   
  82.                             default:  
  83.                                 throw new Win32Exception(error, "Unexpected   error ");  
  84.                         }  
  85.                     }  
  86.                 }  
  87.   
  88.                 Int32 dwPresetIDOffset = BitConverter.ToInt32(outbuff, 0x4);         //   DEVICE_ID.dwPresetIDOffset   
  89.                 Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8);             //   DEVICE_ID.dwPresetSize   
  90.                 Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc);     //   DEVICE_ID.dwPlatformIDOffset   
  91.                 Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10);       //   DEVICE_ID.dwPlatformIDBytes   
  92.                 StringBuilder sb = new StringBuilder();  
  93.   
  94.                 for (int i = dwPresetIDOffset; i < dwPresetIDOffset + dwPresetIDSize; i++)  
  95.                 {  
  96.                     //sb.Append(String.Format( "{0:X2} ",   outbuff[i]));  
  97.                     sb.Append(String.Format("{0} ", outbuff[i]));  
  98.                 }  
  99.   
  100.                 //sb.Append("- ");  
  101.                 //for (int i = dwPlatformIDOffset; i < dwPlatformIDOffset + dwPlatformIDSize; i++)  
  102.                 //{  
  103.                 //    //sb.Append(String.Format( "{0:X2} ",   outbuff[i]));  
  104.                 //    sb.Append(String.Format("{0} ", outbuff[i]));  
  105.   
  106.                 //}  
  107.                 return sb.ToString();   
  108.             }  
  109.             catch (System.Exception e)  
  110.             {  
  111.                 MessageBox.Show("取设备序列号异常"+e.Message );  
  112.                 StringBuilder sb = new StringBuilder();  
  113.                 sb.Append("austec 010-59713137");  
  114.                 return sb.ToString();  
  115.             }  
  116.         }   
  117.   
  118.         public static string GetEncrypt( string strDeviceID )  
  119.         {  
  120.             int i, no;  
  121.               
  122.             StringBuilder   sb   =   new   StringBuilder();  
  123.             for (i = 0; i < strDeviceID.Length; i++)  
  124.             {  
  125.                 try  
  126.                 {  
  127.                     no = int.Parse(strDeviceID[i].ToString());  
  128.                 }  
  129.                 catch (System.Exception ex)  
  130.                 {  
  131.                     no =1;  
  132.                 }  
  133.                  
  134.                 if (no > strEncrypt.Length)  
  135.                     no = no%strEncrypt.Length;  
  136.                 sb.Append(String.Format( "{0}", strEncrypt[no]) );  
  137.             }  
  138.   
  139.             return sb.ToString();  
  140.         }  
  141.     }  
  142. }  


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多