取手持终端的设备ID

  最近在做一些项目时,经常出现客户要求我们送手持终端客户现象.这样,我们只有提高设备的销售价格,但有些客户他们需求10台,在采购2台完成测试后,就因为价格(我们将软件价格分摊到设备)比市面上高,而选择其他供应商,对于这样一种情况,让人很恼火,所以在程序中加入设备ID号的绑定,相关代码如下

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);

        [DllImport(
" CoreDll.dll " )]
        
public   static   extern   int  GetSystemMemoryDivision
            (
            
ref   uint  lpdwStorePages,
            
ref   uint  lpdwRamPages,
            
ref   uint  lpdwPageSize
            );

        [DllImport(
" coredll.dll " )]
        
public   static   extern   void  GetSystemDirectory(StringBuilder SysDir,  int  count);

       
// 获取设备ID号
        public   string  CPUID
        
... {
            
get ...return GetDeviceID().ToString().Trim(); }
        }


        
public    string  GetDeviceID()
        
... {
            
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(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:

                            nBuffSize 
= BitConverter.ToInt32(outbuff, 0);
                            outbuff 
= new byte[nBuffSize];

                            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}", outbuff[i]));
            }


            sb.Append(
"-");

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

            
return sb.ToString();

 

占华

http://www.cardprinterworld.com

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值