Compartilhar via


Como: Obter a plataforma do dispositivo

No .NET Compact Framework versão 3.5 e versões posteriores, você pode usar o WinCEPlatform enumeração para determinar qual plataforma do dispositivo, Pocket PC ou Smartphone, está em execução no momento a seu aplicativo.

No .NET Compact Framework 2,0 ou anterior, você pode retornar a plataforma do dispositivo usando uma invocação de plataforma chamadas para código nativo. O procedimento a seguir define uma classe e uma enumeração das plataformas que você pode usar para determinar a plataforma do dispositivo.

Para obter a plataforma do dispositivo no .NET Compact estrutura 2.0 e versões anteriores

  1. Adicione a seguinte classe e enumeração para o projeto.

    Public Class PlatformDetector
    
        Declare Function SystemParametersInfo Lib "Coredll.dll" _
            (ByVal uiAction As System.UInt32, _
            ByVal uiParam As System.UInt32, _
            ByVal pvParam As StringBuilder, _
            ByVal fWinIni As System.UInt32) As Boolean
    
    
        Private Shared SPI_GETPLATFORMTYPE As System.UInt32 = 257 
    
    
        Public Shared Function GetPlatform() As Platform 
            Dim plat As Platform = Platform.Unknown
            Select Case System.Environment.OSVersion.Platform
                Case PlatformID.Win32NT
                    plat = Platform.Win32NT
                Case PlatformID.Win32S
                    plat = Platform.Win32S
                Case PlatformID.Win32Windows
                    plat = Platform.Win32Windows
                Case PlatformID.WinCE
                    plat = CheckWinCEPlatform()
            End Select
    
            Return plat
    
        End Function 
    
    
        Shared Function CheckWinCEPlatform() As Platform 
            Dim plat As Platform = Platform.WindowsCE
            Dim strbuild As New StringBuilder(200)
            SystemParametersInfo(SPI_GETPLATFORMTYPE, 200, strbuild, 0)
            Dim str As String = strbuild.ToString()
            Select Case str
                Case "PocketPC"
                    plat = Platform.PocketPC
                Case "SmartPhone"
                ' Note that the strbuild parameter from the
                ' PInvoke returns "SmartPhone" with an
                ' upper case P. The correct casing is
                ' "Smartphone" with a lower case p.
                plat = Platform.Smartphone
            End Select
            Return plat
    
        End Function 
    End Class 
    
    
    Public Enum Platform
        PocketPC
        WindowsCE
        Smartphone
        Win32NT
        Win32S
        Win32Windows
        Unknown
    End Enum
    
    public class PlatformDetector
    {
        [DllImport("coredll.dll")]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWinIni);
    
        private static uint SPI_GETPLATFORMTYPE = 257;
    
        public static Platform GetPlatform()
        {
            Platform plat = Platform.Unknown;
            switch (System.Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                    plat = Platform.Win32NT;
                    break;
                case PlatformID.Win32S:
                    plat = Platform.Win32S;
                    break;
                case PlatformID.Win32Windows:
                    plat = Platform.Win32Windows;
                    break;
                case PlatformID.WinCE:
                    plat = CheckWinCEPlatform();
                    break;
            }
    
            return plat;
        }
    
        static Platform CheckWinCEPlatform()
        {
            Platform plat = Platform.WindowsCE;
            StringBuilder strbuild = new StringBuilder(200);
            SystemParametersInfo(SPI_GETPLATFORMTYPE, 200, strbuild, 0);
            string str = strbuild.ToString();
            switch (str)
            {
                case "PocketPC":
                    plat = Platform.PocketPC;
                    break;
                case "SmartPhone":
                    // Note that the strbuild parameter from the
                    // PInvoke returns "SmartPhone" with an
                    // upper case P. The correct casing is
                    // "Smartphone" with a lower case p.
                    plat = Platform.Smartphone;
                    break;
            }
            return plat;
        }
    }
    
    public enum Platform
    {
        PocketPC, WindowsCE, Smartphone, Win32NT, Win32S, Win32Windows, Unknown
    }
    
  2. Adicione a seguinte declaração e código do formulário Load evento.

    Declare Function KernelIoControl Lib "CoreDll.dll" _
        (ByVal dwIoControlCode As Int32, _
         ByVal lpInBuf As IntPtr, _
         ByVal nInBufSize As Int32, _
         ByVal lpOutBuf() As Byte, _
         ByVal nOutBufSize As Int32, _
         ByRef lpBytesReturned As Int32) As Boolean
    
    Private Sub DeviceType_Load(ByVal sender As Object, ByVal e As System.EventArgs)  _
        Handles MyBase.Load
    
           Try
                MessageBox.Show("Platform is " + PlatformDetector.GetPlatform().ToString())
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
            End Try
    
    End Sub
    
    [DllImport("coredll.dll", SetLastError = true)]
    private static extern bool KernelIoControl(Int32 dwIoControlCode,
        IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf,
        Int32 nOutBufSize, ref Int32 lpBytesReturned);
    
    private void DeviceType_Load(object sender, System.EventArgs e)
    {
        try
        {
    
            MessageBox.Show("Platform is " + PlatformDetector.GetPlatform());
        }
    
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
    

Compilando o código

Este exemplo requer referências aos seguintes namespaces:

Consulte também

Tarefas

Como: Obtenha ID do dispositivo e nome

Conceitos

Tópicos "como" do .NET compact estrutura

Outros recursos

Interoperabilidade no .NET Compact Framework