方法 : デバイス ID と名前を取得します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

デバイスの名前を取得、Dns.GetHostName プロパティ使用します。 "PocketPC"には、通常の既定名が。

使用例

次の使用例 ID と名前を表示、デバイスのメッセージ ボックスで、フォームの読み込み時。

デバイス ID、またはシリアル番号を取得するにはプラットフォーム使用する必要があります、ネイティブの Windows CE KernelIoControl 関数への呼び出し。

                        Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Text
Imports Microsoft.VisualBasic

PublicClass DeviceID
    Inherits System.Windows.Forms.Form

    DeclareFunction KernelIoControl Lib"CoreDll.dll" _
        (ByVal dwIoControlCode As Int32, _
        ByVal lpInBuf As IntPtr, _
        ByVal nInBufSize As Int32, _
        ByVal lpOutBuf() AsByte, _
        ByVal nOutBufSize As Int32, _
        ByRef lpBytesReturned As Int32) AsBooleanPublicSubNew()

        Me.Text = "DeviceID"
        ' Display OK close button.Me.MinimizeBox = FalseEndSubProtectedOverloadsOverridesSub Dispose(ByVal disposing AsBoolean)
        MyBase.Dispose(disposing)
    EndSubSharedSub Main()
        Application.Run(New DeviceID)
    EndSubPrivateShared METHOD_BUFFERED As Int32 = 0
    PrivateShared FILE_ANY_ACCESS As Int32 = 0
    PrivateShared FILE_DEVICE_HAL As Int32 = &H101

    PrivateConst ERROR_NOT_SUPPORTED As Int32 = &H32
    PrivateConst ERROR_INSUFFICIENT_BUFFER As Int32 = &H7A

    PrivateShared IOCTL_HAL_GET_DEVICEID As Int32 = _
        (&H10000 * FILE_DEVICE_HAL) Or (&H4000 * FILE_ANY_ACCESS) _
        Or (&H4 * 21) Or METHOD_BUFFERED

    PrivateSharedFunction GetDeviceID() AsString
        ' Initialize the output buffer to the size of a         ' Win32 DEVICE_ID structure Dim outbuff(19) AsByteDim dwOutBytes As Int32
        Dim done AsBoolean = FalseDim nBuffSize As Int32 = outbuff.Length

        ' Set DEVICEID.dwSize to size of buffer.  Some platforms look at        ' this field rather than the nOutBufSize param of KernelIoControl        ' when determining if the buffer is large enough.
        BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0)
        dwOutBytes = 0

        ' Loop until the device ID is retrieved or an error occurs.WhileNot done
            If KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, _
                0, outbuff, nBuffSize, dwOutBytes) Then
                done = TrueElseDim errnum AsInteger = Marshal.GetLastWin32Error()
                SelectCase errnum
                    Case ERROR_NOT_SUPPORTED
                        ThrowNew NotSupportedException( _
                            "IOCTL_HAL_GET_DEVICEID is not supported on this device", _
                            New Win32Exception(errnum))

                    Case ERROR_INSUFFICIENT_BUFFER

                        ' The buffer is not big enough for the data.  The                        ' required size is in the first 4 bytes of the output                         ' buffer (DEVICE_ID.dwSize).
                        nBuffSize = BitConverter.ToInt32(outbuff, 0)
                        outbuff = NewByte(nBuffSize) {}

                        ' Set DEVICEID.dwSize to size of buffer.  Some                        ' platforms look at this field rather than the                        ' nOutBufSize param of KernelIoControl when                        ' determining if the buffer is large enough.
                        BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0)

                    CaseElseThrowNew Win32Exception(errnum, "Unexpected error")
                EndSelectEndIfEndWhile
        ' Copy the elements of the DEVICE_ID structure.Dim dwPresetIDOffset As Int32 = BitConverter.ToInt32(outbuff, &H4)
        Dim dwPresetIDSize As Int32 = BitConverter.ToInt32(outbuff, &H8)
        Dim dwPlatformIDOffset As Int32 = BitConverter.ToInt32(outbuff, &HC)
        Dim dwPlatformIDSize As Int32 = BitConverter.ToInt32(outbuff, &H10)
        Dim sb AsNew StringBuilder
        Dim i AsIntegerFor i = dwPresetIDOffset To (dwPresetIDOffset + dwPresetIDSize) - 1
            sb.Append(String.Format("{0:X2}", outbuff(i)))
        Next i

        sb.Append("-")

        For i = dwPlatformIDOffset To (dwPlatformIDOffset + dwPlatformIDSize) - 1
            sb.Append(String.Format("{0:X2}", outbuff(i)))
        Next i

        Return sb.ToString()
    EndFunctionPrivateSub Form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMyBase.Load
        Try            ' Show the device ID.Dim deviceID AsString = GetDeviceID()
            MessageBox.Show("Device ID: " + deviceID)

            ' Show the device name.Dim deviceName AsString = System.Net.Dns.GetHostName()
            MessageBox.Show("Device Name: " & deviceName)

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        EndTryEndSubEndClass
                        using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

namespace DeviceID
{
    /// <summary>/// Summary description for DeviceID./// </summary>publicclass DeviceID : System.Windows.Forms.Form
    {

    public DeviceID()
        {
            //// Required for Windows Form Designer support//
            InitializeComponent();

            //// TODO: Add any constructor code after InitializeComponent call//
        }

        /// <summary>/// Clean up any resources being used./// </summary>protectedoverridevoid Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>privatevoid InitializeComponent()
        {
            // // DeviceID// this.Text = "DeviceID";
            this.Load += new System.EventHandler(this.DeviceID_Load);

        }
        staticvoid Main() 
        {
            Application.Run(new DeviceID());
        }
        #endregion

        privatestatic Int32 METHOD_BUFFERED = 0;
        privatestatic Int32 FILE_ANY_ACCESS = 0;
        privatestatic Int32 FILE_DEVICE_HAL = 0x00000101;

        privateconst Int32 ERROR_NOT_SUPPORTED = 0x32;
        privateconst Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;

        privatestatic Int32 IOCTL_HAL_GET_DEVICEID = 
            ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) 
            | ((21) << 2) | (METHOD_BUFFERED);

        [DllImport("coredll.dll", SetLastError=true)]
        privatestaticexternbool KernelIoControl(Int32 dwIoControlCode, 
            IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, 
            Int32 nOutBufSize, ref Int32 lpBytesReturned);

        privatestaticstring GetDeviceID()
        {
            // Initialize the output buffer to the size of a // Win32 DEVICE_ID structure.byte[] outbuff = newbyte[20];
            Int32  dwOutBytes;
            bool done = false;

            Int32 nBuffSize = outbuff.Length;

            // Set DEVICEID.dwSize to size of buffer.  Some platforms look at// this field rather than the nOutBufSize param of KernelIoControl// when determining if the buffer is large enough.
            BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);  
            dwOutBytes = 0;

            // Loop until the device ID is retrieved or an error occurs.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:
                        thrownew NotSupportedException(
                            "IOCTL_HAL_GET_DEVICEID is not supported on this device",
                            new Win32Exception(error));

                    case ERROR_INSUFFICIENT_BUFFER:

                        // The buffer is not big enough for the data.  The// required size is in the first 4 bytes of the output// buffer (DEVICE_ID.dwSize).
                        nBuffSize = BitConverter.ToInt32(outbuff, 0);
                        outbuff = newbyte[nBuffSize];

                        // Set DEVICEID.dwSize to size of buffer.  Some// platforms look at this field rather than the// nOutBufSize param of KernelIoControl when// determining if the buffer is large enough.
                        BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
                        break;

                    default:
                        thrownew Win32Exception(error, "Unexpected error");
                    }
                }
            }

            // Copy the elements of the DEVICE_ID structure.
            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();
        }

        privatevoid DeviceID_Load(object sender, System.EventArgs e)
        {
            try 
            {
                // Show the device ID.string strDeviceID = GetDeviceID();
                MessageBox.Show("Device ID: " + strDeviceID);

                // Show the device name.string deviceName = System.Net.Dns.GetHostName();
                MessageBox.Show("Device Name: " + deviceName);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

堅牢性の高いプログラム

次の表は、ネイティブ KernelIoControl 関数パラメーターです。 すべての設定は 32 ビットです。

引数

Win32 の種類

マネージ型

一般的な値

dwIoControlCode

DWORD

Int32

IOCTL_HAL_GET_DEVICEID

lpInBuf

LPVOID

IntPtr

IntPtr.Zero (必要ない入力データ)

nInBufSize

DWORD

Int32

0 (必要ない入力データ)

lpOutBuf

LPVOID*

Int32

20-要素 Byte 配列を (20 バイトでは、 DEVICE_ID 構造体のサイズ)

nOutBufSize

DWORD

Int32

20

lpBytesReturned

LPWORD

ref Int32

0

lpOutBuf パラメーターには、次のような構造をが。

Structure DEVICE_ID
    Private dwSize As Integer
    Private dwPresetIDOffset As Integer
    Private dwPresetIDBytes As Integer
    Private dwPlatformIDOffset As Integer
    Private dwPlatformIDBytes As Integer
End Structure
struct DEVICE_ID
{
    int dwSize;
    int dwPresetIDOffset;
    int dwPresetIDBytes;
    int dwPlatformIDOffset;
    int dwPlatformIDBytes;
}

戻り値とエラー処理

KernelIoControl 関数を返します true デバイス ID が、出力バッファーにコピーされた場合それ以外の false を返します。 KernelIoControl が失敗すると、Win32 エラー コードを取得するマネージ GetLastWin32Error メソッドを呼び出します。 エラー コードは、次のいずれかであります。

  • ERROR_NOT_SUPPORTED - は、デバイスが、IOCTL_HAL_GET_DEVICEID コントロールのコードを実装しないことを示します。

  • ERROR_INSUFFICIENT_BUFFER - を出力バッファーをデバイスの ID。 を保持できる大きさしないことを示します dwSize DEVICE_ID 構造内で指定されたバイト数、必要なは出力バッファーの最初の 4 バイトで返されます。 このエラーが発生した場合 dwSize で指定されたサイズへの出力バッファーを再割り当てもう一度 KernelIoControl を呼び出してください。

参照

処理手順

方法 : デバイス メモリを取得します。

その他の技術情報

.NET Compact Framework の相互運用性