Results 1 to 4 of 4

Thread: Get serial number and model number

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    29

    Get serial number and model number

    I am trying to get the serial number and model number for my mobile device.

    I am not looking for the device ID I am looking for the serial number my device displays when I go to System config

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Get serial number and model number

    Hi,
    try this...
    Code:
    Imports System.Text
    Imports System.Runtime.InteropServices
    Imports System.ComponentModel
    
    Public Class frmMain
    
        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
        Public Const ERROR_NOT_SUPPORTED As Int32 = &H32
        Public Const ERROR_INSUFFICIENT_BUFFER As Int32 = &H7A
        Public Const IOCTL_HAL_GET_DEVICEID As Int32 = (&H10000 * FILE_DEVICE_HAL) Or (&H4000 * FILE_ANY_ACCESS) Or (&H4 * 21) Or METHOD_BUFFERED
    
        Private Const METHOD_BUFFERED As Int32 = 0
        Private Const FILE_ANY_ACCESS As Int32 = 0
        Private Const FILE_DEVICE_HAL As Int32 = &H101
        Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
            Me.Close()
        End Sub
    
        Private Sub btnID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnID.Click
            lblDevice.Text = ReadDeviceID()
            If lblDevice.Text.Length <> 0 Then
                Dim sw As New System.IO.StreamWriter("My Documents\DeviceSerial.txt", False)
                sw.WriteLine(lblDevice.Text)
                sw.Flush()
                sw.Close()
                MessageBox.Show("Device ID saved to \My Documents\DeviceSerial.txt", "Saved", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1)
            End If
        End Sub
    
        Public Shared Function ReadDeviceID() As String
            ' Initialize the output buffer to the size of a 
            ' Win32 DEVICE_ID structure 
            Dim outbuff(31) As Byte
            Dim dwOutBytes As Int32
            Dim done As Boolean = False
            Dim nBuffSize As Int32 = outbuff.Length
            Dim dwPresetIDOffset As Int32
            Dim dwPresetIDSize As Int32
            Dim dwPlatformIDOffset As Int32
            Dim dwPlatformIDSize As Int32
            Dim sb As New StringBuilder
            Dim i As Integer
            Dim rtnStr As String = String.Empty
    
            Try
                ' 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 Not done
                    If KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, _
                        0, outbuff, nBuffSize, dwOutBytes) Then
                        done = True
                    Else
                        Dim errnum As Integer = Marshal.GetLastWin32Error()
                        Select Case errnum
                            Case ERROR_NOT_SUPPORTED
                                Throw New 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 = New Byte(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)
    
                            Case Else
                                Throw New Win32Exception(errnum, "Unexpected error")
                        End Select
                    End If
                End While
    
                ' Copy the elements of the DEVICE_ID structure.
                dwPresetIDOffset = BitConverter.ToInt32(outbuff, &H4)
                dwPresetIDSize = BitConverter.ToInt32(outbuff, &H8)
                dwPlatformIDOffset = BitConverter.ToInt32(outbuff, &HC)
                dwPlatformIDSize = BitConverter.ToInt32(outbuff, &H10)
    
                For i = dwPresetIDOffset To (dwPresetIDOffset + dwPresetIDSize) - 1
                    sb.Append(String.Format("{0:X2}", outbuff(i)))
                Next i
    
                'leave out the - from the serial number
                'sb.Append("-")
    
                For i = dwPlatformIDOffset To (dwPlatformIDOffset + dwPlatformIDSize) - 1
                    sb.Append(String.Format("{0:X2}", outbuff(i)))
                Next i
            Catch ex As Exception
                If IsNothing(sb) = False Then
                    sb = Nothing
                End If
            Finally
                If IsNothing(sb) = False Then
                    If (sb.ToString().IndexOf("-") > 0) Then
                        'rtnStr = sb.ToString().Substring(0, sb.ToString().IndexOf("-"))
                        ' leave out the - from the serial number
                        rtnStr = sb.ToString().Replace("-", "")
                    Else
                        rtnStr = sb.ToString()
                    End If
                Else
                    rtnStr = "-"
                End If
            End Try
            Return rtnStr
        End Function
    
    End Class
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Get serial number and model number

    @ Pete

    Is that code used as is or do I need to add something more? I am not familiar with coding in VB.Net only C#.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Get serial number and model number

    That should be it - it should work as is.
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width