Monday, November 23, 2009

Get IMEI IMSI number of the mobile phone - wince smart device mobile application

Have you wondered how to fetch the IMEI or IMSI number from the winows enabled mobile phone through dot net!!!

Yes there is a solution. I dont know from where i have found out the code , definitely the credit goes to the author.


create a class file named "DevicePhoneInfo.vb"

********************* START **************************************

Imports System

Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Xml
Imports System.IO

Namespace TAPI
Public Module PhoneInfo
'Private Sub New()
'End Sub
'original code location:
'http://www.developersdex.com/vb/message.asp?p=2916&ID=%3C68D1F07B-ECB0-4A15-AFCA-2A911FC3234C%40microsoft.com%3E
Public Sub [Get](ByRef manufacturer As String, ByRef model As String, ByRef revision As String, ByRef serialNumber As String, ByRef subsciberId As String)
Dim hLine As IntPtr
Dim dwNumDev As Integer
Dim num1 As Integer = &H20000
Dim lineInitializeParams As New LINEINITIALIZEEXPARAMS()
lineInitializeParams.dwTotalSize = CUInt(Marshal.SizeOf(lineInitializeParams))
lineInitializeParams.dwNeededSize = lineInitializeParams.dwTotalSize
lineInitializeParams.dwOptions = 2
lineInitializeParams.hEvent = IntPtr.Zero
lineInitializeParams.hCompletionPort = IntPtr.Zero
'#Region "lineInitializeEx"
Dim result As Integer = Tapi.lineInitializeEx(hLine, IntPtr.Zero, IntPtr.Zero, Nothing, dwNumDev, num1, _
lineInitializeParams)
If result <> 0 Then
Throw New ApplicationException(String.Format("lineInitializeEx failed!" & vbLf & vbLf & "Error Code:{0}", result.ToString()))
End If
'#End Region
'#Region "lineNegotiateAPIVerison"
Dim version As Integer
Dim dwAPIVersionLow As Integer = &H10004
Dim dwAPIVersionHigh As Integer = &H20000
Dim lineExtensionID As LINEEXTENSIONID
result = Tapi.lineNegotiateAPIVersion(hLine, 0, dwAPIVersionLow, dwAPIVersionHigh, version, lineExtensionID)
If result <> 0 Then
Throw New ApplicationException(String.Format("lineNegotiateAPIVersion failed!" & vbLf & vbLf & "Error Code: {0}", result.ToString()))
End If
'#End Region

'#Region "lineOpen"
Dim hLine2 As IntPtr = IntPtr.Zero
result = Tapi.lineOpen(hLine, 0, hLine2, version, 0, IntPtr.Zero, _
&H2, &H4, IntPtr.Zero)
If result <> 0 Then
Throw New ApplicationException(String.Format("lineNegotiateAPIVersion failed!" & vbLf & vbLf & "Error Code: {0}", result.ToString()))
End If
'#End Region

'#Region "lineGetGeneralInfo"
Dim structSize As Integer = Marshal.SizeOf(New LINEGENERALINFO())
Dim bytes As Byte() = New Byte(structSize - 1) {}
Dim tmpBytes As Byte() = BitConverter.GetBytes(structSize)

For index As Integer = 0 To tmpBytes.Length - 1
bytes(index) = tmpBytes(index)
Next
'#End Region

'#Region "make initial query to retrieve necessary size"
result = Tapi.lineGetGeneralInfo(hLine2, bytes)

' get the needed size
Dim neededSize As Integer = BitConverter.ToInt32(bytes, 4)

' resize the array
bytes = New Byte(neededSize - 1) {}

' write out the new allocated size to the byte stream
tmpBytes = BitConverter.GetBytes(neededSize)
For index As Integer = 0 To tmpBytes.Length - 1
bytes(index) = tmpBytes(index)
Next

' fetch the information with properly size buffer
result = Tapi.lineGetGeneralInfo(hLine2, bytes)

If result <> 0 Then
Throw New ApplicationException(Marshal.GetLastWin32Error().ToString())
End If

'#End Region

'#Region "actual data fetching"
Dim size As Integer
Dim offset As Integer

' manufacture
size = BitConverter.ToInt32(bytes, 12)
offset = BitConverter.ToInt32(bytes, 16)
manufacturer = Encoding.Unicode.GetString(bytes, offset, size)
manufacturer = manufacturer.Substring(0, manufacturer.IndexOf(ControlChars.NullChar))

' model
size = BitConverter.ToInt32(bytes, 20)
offset = BitConverter.ToInt32(bytes, 24)
model = Encoding.Unicode.GetString(bytes, offset, size)
model = model.Substring(0, model.IndexOf(ControlChars.NullChar))

' revision
size = BitConverter.ToInt32(bytes, 28)
offset = BitConverter.ToInt32(bytes, 32)
revision = Encoding.Unicode.GetString(bytes, offset, size)
revision = revision.Substring(0, revision.IndexOf(ControlChars.NullChar))

' serial number
size = BitConverter.ToInt32(bytes, 36)
offset = BitConverter.ToInt32(bytes, 40)
serialNumber = Encoding.Unicode.GetString(bytes, offset, size)
serialNumber = serialNumber.Substring(0, serialNumber.IndexOf(ControlChars.NullChar))

' subscriber id
size = BitConverter.ToInt32(bytes, 44)
offset = BitConverter.ToInt32(bytes, 48)
subsciberId = Encoding.Unicode.GetString(bytes, offset, size)
subsciberId = subsciberId.Substring(0, subsciberId.IndexOf(ControlChars.NullChar))

'#End Region


'tear down
Tapi.lineClose(hLine2)

Tapi.lineShutdown(hLine)
End Sub
End Module

End Namespace
 
********************* END ****************************************
 
create another class named "TAPI.vb"
 
********************* START **************************************
 
  Imports System

Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Xml
Imports System.IO

Namespace TAPI
Public Module PhoneInfo
'Private Sub New()
'End Sub
'original code location:
'http://www.developersdex.com/vb/message.asp?p=2916&ID=%3C68D1F07B-ECB0-4A15-AFCA-2A911FC3234C%40microsoft.com%3E
Public Sub [Get](ByRef manufacturer As String, ByRef model As String, ByRef revision As String, ByRef serialNumber As String, ByRef subsciberId As String)
Dim hLine As IntPtr
Dim dwNumDev As Integer
Dim num1 As Integer = &H20000
Dim lineInitializeParams As New LINEINITIALIZEEXPARAMS()
lineInitializeParams.dwTotalSize = CUInt(Marshal.SizeOf(lineInitializeParams))
lineInitializeParams.dwNeededSize = lineInitializeParams.dwTotalSize
lineInitializeParams.dwOptions = 2
lineInitializeParams.hEvent = IntPtr.Zero
lineInitializeParams.hCompletionPort = IntPtr.Zero
'#Region "lineInitializeEx"
Dim result As Integer = Tapi.lineInitializeEx(hLine, IntPtr.Zero, IntPtr.Zero, Nothing, dwNumDev, num1, _
lineInitializeParams)
If result <> 0 Then
Throw New ApplicationException(String.Format("lineInitializeEx failed!" & vbLf & vbLf & "Error Code:{0}", result.ToString()))
End If
'#End Region
'#Region "lineNegotiateAPIVerison"
Dim version As Integer
Dim dwAPIVersionLow As Integer = &H10004
Dim dwAPIVersionHigh As Integer = &H20000
Dim lineExtensionID As LINEEXTENSIONID
result = Tapi.lineNegotiateAPIVersion(hLine, 0, dwAPIVersionLow, dwAPIVersionHigh, version, lineExtensionID)
If result <> 0 Then
Throw New ApplicationException(String.Format("lineNegotiateAPIVersion failed!" & vbLf & vbLf & "Error Code: {0}", result.ToString()))
End If
'#End Region

'#Region "lineOpen"
Dim hLine2 As IntPtr = IntPtr.Zero
result = Tapi.lineOpen(hLine, 0, hLine2, version, 0, IntPtr.Zero, _
&H2, &H4, IntPtr.Zero)
If result <> 0 Then
Throw New ApplicationException(String.Format("lineNegotiateAPIVersion failed!" & vbLf & vbLf & "Error Code: {0}", result.ToString()))
End If
'#End Region

'#Region "lineGetGeneralInfo"
Dim structSize As Integer = Marshal.SizeOf(New LINEGENERALINFO())
Dim bytes As Byte() = New Byte(structSize - 1) {}
Dim tmpBytes As Byte() = BitConverter.GetBytes(structSize)

For index As Integer = 0 To tmpBytes.Length - 1
bytes(index) = tmpBytes(index)
Next
'#End Region

'#Region "make initial query to retrieve necessary size"
result = Tapi.lineGetGeneralInfo(hLine2, bytes)

' get the needed size
Dim neededSize As Integer = BitConverter.ToInt32(bytes, 4)

' resize the array
bytes = New Byte(neededSize - 1) {}

' write out the new allocated size to the byte stream
tmpBytes = BitConverter.GetBytes(neededSize)
For index As Integer = 0 To tmpBytes.Length - 1
bytes(index) = tmpBytes(index)
Next

' fetch the information with properly size buffer
result = Tapi.lineGetGeneralInfo(hLine2, bytes)

If result <> 0 Then
Throw New ApplicationException(Marshal.GetLastWin32Error().ToString())
End If

'#End Region

'#Region "actual data fetching"
Dim size As Integer
Dim offset As Integer

' manufacture
size = BitConverter.ToInt32(bytes, 12)
offset = BitConverter.ToInt32(bytes, 16)
manufacturer = Encoding.Unicode.GetString(bytes, offset, size)
manufacturer = manufacturer.Substring(0, manufacturer.IndexOf(ControlChars.NullChar))

' model
size = BitConverter.ToInt32(bytes, 20)
offset = BitConverter.ToInt32(bytes, 24)
model = Encoding.Unicode.GetString(bytes, offset, size)
model = model.Substring(0, model.IndexOf(ControlChars.NullChar))

' revision
size = BitConverter.ToInt32(bytes, 28)
offset = BitConverter.ToInt32(bytes, 32)
revision = Encoding.Unicode.GetString(bytes, offset, size)
revision = revision.Substring(0, revision.IndexOf(ControlChars.NullChar))

' serial number
size = BitConverter.ToInt32(bytes, 36)
offset = BitConverter.ToInt32(bytes, 40)
serialNumber = Encoding.Unicode.GetString(bytes, offset, size)
serialNumber = serialNumber.Substring(0, serialNumber.IndexOf(ControlChars.NullChar))

' subscriber id
size = BitConverter.ToInt32(bytes, 44)
offset = BitConverter.ToInt32(bytes, 48)
subsciberId = Encoding.Unicode.GetString(bytes, offset, size)
subsciberId = subsciberId.Substring(0, subsciberId.IndexOf(ControlChars.NullChar))

'#End Region


'tear down
Tapi.lineClose(hLine2)

Tapi.lineShutdown(hLine)
End Sub
End Module

End Namespace
 
********************* END **************************************

create a form named "getIMEI_IMSI.vb"  , in the code behind  paste the following code

*********************  START  **************************************
Imports System


Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Text

Imports System.Windows.Forms

Imports Camera2.TAPI



Public Class getIMEI_IMSI



Private Sub getIMEI_IMSI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim manifactrer As String

Dim model As String

Dim revision As String

Dim imsi As String

Dim imei As String



PhoneInfo.[Get](manifactrer, model, revision, imei, imsi)



TextBox1.Text = imsi



TextBox2.Text = imei

End Sub

End Class
 
********************* END **************************************

No comments:

Post a Comment