GetDeviceUniqueID 関数を使って、デバイスのID(シリアル番号)を取得する
今回はデバイスのシリアル番号を取得する方法をご紹介します。
GetDeviceUniqueID 関数を使用して、デバイスのID(シリアル番号)を取得する事が出来ます。
デバイス唯一のIDを取得する事が出来るので、アプリのソフトウェアライセンスを紐付ける事も可能です。
KernelIoControl(IOCTL_HAL_GET_DEVICEID)による違いは、セキュリティの面から見て端末IDを保護する事にあります。詳しくはWindows Mobile 開発チームのブログを見ればいいよ。
サンプルコードを以下に示します。
ネイティブ関数とやり取りする為の構造体を定義
<DllImport("coredll.dll")> _
Private Shared Function GetDeviceUniqueID( _
ByRef appdata As Byte(), _
ByVal cbApplictionData As Integer, _
ByVal dwDeviceIDVersion As Integer, _
ByRef deviceIDOuput As Byte(), _
ByRef pcbDeviceIDOutput As UInteger) As Integer
End Function
// 以下の名前空間を指定しておいてください
// Imports System.Runtime.InteropServices
デバイスのID(シリアル番号)を取得する
Private Function GetDeviceID(ByVal AppString As String) As Byte()
Dim DeviceIDLength As Integer = 0
Dim AppData As Byte() = _
System.Text.Encoding.Unicode.GetBytes(AppString)
Dim appDataSize As Integer = AppData.Length
Dim DeviceOutput(DeviceIDLength - 1) As Byte
Dim SizeOut As UInteger = DeviceIDLength
' デバイス毎の一意のIDを取得します
GetDeviceUniqueID(AppData, appDataSize, 1, DeviceOutput, SizeOut)
Return DeviceOutput
End Function
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim id() As Byte = GetDeviceID("myAppName")
End Sub
// 以下の名前空間を指定しておいてください
// Imports System.Runtime.InteropServices