今回は、デバイスのシリアル番号を取得する方法をご紹介します。
GetDeviceUniqueID関数を使用して、デバイスのID(シリアル番号)を取得する事が出来ます。デバイス唯一のIDを取得する事が出来るので、アプリのソフトウェアライセンスを紐付ける事も可能です。
KernelIoControl(IOCTL_HAL_GET_DEVICEID)による違いは、セキュリティの面から見て端末IDを保護する事にあります。詳しくはWindows Mobile 開発チームのブログを見てください。
サンプルコードを以下に示します。
GetDeviceUniqueID 関数を使って、デバイスのID(シリアル番号)を取得する
VB.NET
ネイティブ関数とやり取りする為の構造体を定義。
// 以下の名前空間を指定しておいてください // Imports System.Runtime.InteropServices <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
デバイスのID(シリアル番号)を取得する。
Private Function GetDeviceID(ByVal AppString As String) As Byte() Dim DeviceIDLength As Integer = 20 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
C#
ネイティブ関数とやり取りする為の構造体を定義。
[System.Runtime.InteropServices.DllImport("coredll.dll")] private static int GetDeviceUniqueID( byte[] appdata, int cbApplictionData, int cbApplictionData, byte[] deviceIDOuput, uint pcbDeviceIDOutput);
デバイスのID(シリアル番号)を取得する。
private byte[] GetDeviceID(string appString) { byte[] appData = System.Text.Encoding.Unicode.GetBytes(appString); int appDataSize = appData.Length; byte[] DeviceOutput = new byte[20]; uint SizeOut = 20; // デバイス毎の一意のIDを取得します GetDeviceUniqueID(appData, appDataSize, 1, DeviceOutput, SizeOut); return DeviceOutput; }