mobile中如何取得设备ID

 mobile中如何取得设备ID呢?  

请看下面的代码(实在懒的写太多的话了^_^)

Declare   Function KernelIoControl Lib "coredll" (ByVal dwIoControlCode As IntegerByVal lpInBuf As IntPtr, ByVal nInBufSize As IntegerByVal lpOutBuf() As ByteByVal nOutBufSize As IntegerByRef lpBytesReturned As IntegerAs Integer

    
Private Const IOCTL_HAL_GET_DEVICEID As Int32 = 16842836

    
Public Function GetSerialNumberFromKernelIoControl() As String
        
Dim dwOutBytes As Integer
        
Dim nBuffSize As Integer
        
Dim strDeviceId As String
        nBuffSize 
= 4096
        
Dim arrOutBuff(4096As Byte
        
Dim res As Boolean
        res 
= KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, arrOutBuff, nBuffSize, dwOutBytes)
        
If res <> False Then
            
MsgBox(res.ToString)
            
MsgBox(dwOutBytes.ToString)
            
Dim strDeviceInfo As String = ""
            
Dim i As Integer
            
Dim strNextChar As String = ""

            
For i = 1 To dwOutBytes
                strNextChar 
= arrOutBuff(i).ToString
                strDeviceInfo 
= strDeviceInfo + strNextChar
            
Next

            strDeviceId 
= strDeviceInfo

            
Return strDeviceId
        
End If
    
End Function

 再添加一个c#版的

 

public   class  GetDeviceInf
    
{
        [DllImport(
"coredll.dll")]
        
private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr
            InputBuffer, Int32 InputBufferSize, 
byte[] OutputBuffer, Int32
            OutputBufferSize, 
ref Int32 BytesReturned);

        
private static Int32 FILE_DEVICE_HAL = 0x00000101;
        
private static Int32 FILE_ANY_ACCESS = 0x0;
        
private static Int32 METHOD_BUFFERED = 0x0;

        
private static Int32 IOCTL_HAL_GET_DEVICEID =
            ((FILE_DEVICE_HAL) 
<< 16| ((FILE_ANY_ACCESS) << 14)
            
| ((21<< 2| (METHOD_BUFFERED);
        
private static string GetDeviceID()
        
{
            
byte[] OutputBuffer = new byte[256];
            Int32 OutputBufferSize, BytesReturned;
            OutputBufferSize 
= OutputBuffer.Length;
            BytesReturned 
= 0;

            
// Call KernelIoControl passing the previously defined
            
// IOCTL_HAL_GET_DEVICEID parameter
            
// We don't need to pass any input buffers to this call
            
// so InputBuffer and InputBufferSize are set to their null
            
// values
            
// 运行上面定义的函数 KernelIoControl
            
// 并传递前面已经定义了的静态变量作为参数
            
// 我们不需要输入任何缓存到这个函数
            
// 因此InputBuffer和InputBufferSize两个形参传递实参null
           
            
bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID,
                IntPtr.Zero,
                
0,
                OutputBuffer,
                OutputBufferSize,
                
ref BytesReturned);

            
// If the request failed, exit the method now
            
// 如果请求失败,立刻退出方法
            if (retVal == false)
            
{
                
return null;
            }


            
// Examine the OutputBuffer byte array to find the start of the 
            
// Preset ID and Platform ID, as well as the size of the
            
// PlatformID. 
            
//检查输出数组缓存去寻找 Platform ID以及PlatformID.
        

            
// PresetIDOffset -The number of bytes the preset ID is offset
            
//                  from the beginning of the structure
            
// PlatformIDOffset - The number of bytes the platform ID is
            
//                    offset from the beginning of the structure
            
// PlatformIDSize - The number of bytes used to store the
            
//                  platform ID
            
// Use BitConverter.ToInt32() to convert from byte[] to int
            Int32 PresetIDOffset = BitConverter.ToInt32(OutputBuffer, 4);
            Int32 PlatformIDOffset 
= BitConverter.ToInt32(OutputBuffer, 0xc);
            Int32 PlatformIDSize 
= BitConverter.ToInt32(OutputBuffer, 0x10);

            
//将Preset ID转换成string便于显示
            
// Convert the Preset ID segments into a string so they can be 
            
// displayed easily.
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append
                (
                String.Format(
"{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
                BitConverter.ToInt32(OutputBuffer, PresetIDOffset),
                BitConverter.ToInt16(OutputBuffer, PresetIDOffset 
+ 4),
                BitConverter.ToInt16(OutputBuffer, PresetIDOffset 
+ 6),
                BitConverter.ToInt16(OutputBuffer, PresetIDOffset 
+ 8))
                );

            
//将Platform ID分解成16进制的数字附加到Preset ID上
            
//这将形成一个格式化了的Device ID字符串
            
// Break the Platform ID down into 2-digit hexadecimal numbers
            
// and append them to the Preset ID. This will result in a 
            
// string-formatted Device ID
            for (int i = PlatformIDOffset;
                i 
< PlatformIDOffset + PlatformIDSize;
                i
++)
            
{
                sb.Append(String.Format(
"{0:X2}", OutputBuffer[i]));
            }


            
// return the Device ID string
            
//返回Device ID字符串
            return sb.ToString();
        }

    }

 

HUST:Eric

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值