如何抓取通話紀錄(mobile)

在上一篇有提到如何抓取SIM卡中連絡人的相關資料,這篇我們來測試一下抓取通話紀錄的部分。

在上一篇有提到如何抓取SIM卡中連絡人的相關資料,這篇我們來測試一下抓取通話紀錄的部分。首先,下面測試的程式碼是參考自Code Project的Retrieving and Storing Call History,詳細的資料以及原始碼可以到code project去下載,下載之後解開壓縮,你會看到一個WindowsMobile6的資料夾,存取calllog相關的程式碼都在裡面,可以參考,不過程式碼都是C#的就是了。在論壇上關於取得通話記錄的問題這篇也可以找到相關討論。 好,下面來看看實作的程式碼吧。

首先是模組的程式碼

Imports System.Runtime.InteropServices

Module Module2
    <DllImport("Phone.dll")> _
    Public Function PhoneOpenCallLog(ByRef ph As IntPtr) As IntPtr
    End Function

    <DllImport("Phone.dll")> _
    Public Function PhoneGetCallLogEntry(ByVal h As IntPtr, ByRef pentry As CALLLOGENTRY) As IntPtr
    End Function

    <DllImport("Phone.dll")> _
    Public Function PhoneCloseCallLog(ByVal h As IntPtr) As IntPtr
    End Function

    <DllImport("Phone.dll")> _
    Public Function PhoneSeekCallLog(ByVal hdb As IntPtr, ByVal seek As CALLLOGSEEK, ByVal iRecord As UInteger, _
                                     ByRef piRecord As UInteger) As IntPtr
    End Function

    <StructLayout(LayoutKind.Explicit, Size:=48)> _
    Structure CALLLOGENTRY
        <FieldOffset(0)> _
        Public cbSize As UInteger
        <FieldOffset(4)> _
        Public ftStartTime As Long
        <FieldOffset(12)> _
        Public ftEndTime As Long
        <FieldOffset(20)> _
        Public iom As enumIOM
        <FieldOffset(24)> _
        Public flags As fFlags
        <FieldOffset(28)> _
        Public cidt As CALLERIDTYPE <FieldOffset(32)> _ Public pszNumber As IntPtr
        <FieldOffset(36)> _
        Public pszName As IntPtr
        <FieldOffset(40)> _
        Public pszNameType As IntPtr
        <FieldOffset(44)> _
        Public pszNote As IntPtr
    End Structure

    Public Enum CALLERIDTYPE
        UNAVAILABLE
        BLOCKED
        AVAILABLE
    End Enum

    Public Enum enumIOM
        MISSED
        INCOMING
        OUTGOING
    End Enum

    <Flags()> _
     Public Enum fFlags As Byte
        Outgoing = &H1
        Connected = &H2
        Ended = &H4
        Roam = &H8
    End Enum

    Public Enum CALLLOGSEEK
        CALLLOGSEEK_BEGINNING = 2
        CALLLOGSEEK_END = 4
    End Enum
           
End Module
 

叫用範例

Public Sub enumCallLog()
        Dim h As IntPtr
        Dim entry As CALLLOGENTRY
        Dim LastlogIndex As UInteger = 0
        Dim CurrentlogIndex As UInteger = 0

        entry = New CALLLOGENTRY()
        entry.cbSize = Marshal.SizeOf(entry.GetType)

        PhoneOpenCallLog(h)
        If PhoneSeekCallLog(h, CALLLOGSEEK.CALLLOGSEEK_END, 0, LastlogIndex) = 0 Then
            For i As Integer = 0 To LastlogIndex - 1
                PhoneSeekCallLog(h, CALLLOGSEEK.CALLLOGSEEK_BEGINNING, i, CurrentlogIndex)
                PhoneGetCallLogEntry(h, entry)
                ListBox1.Items.Add(Marshal.PtrToStringUni(entry.pszName) & "," & _
                                   Marshal.PtrToStringUni(entry.pszNumber))
                Application.DoEvents()
            Next
        End If
    End Sub