1注意:本文内容来自Windows Mobile Team Blog。代码部分经过测试(在上海移动网络,易通卡),未能获取到电话号码,可能是服务商不提供该服务。感兴趣的朋友可以参考一下。
  2#include "stdafx.h"
  3#include "windows.h"
  4#include "tapi.h"
  5#include "tsp.h"
  6#pragma comment(lib,"cellcore.lib")
  7
  8#define EXIT_ON_NULL(_p)     \
  9    if (_p == NULL)    \
 10{   \
 11    hr = E_OUTOFMEMORY; \
 12    goto FuncExit; \
 13
 14
 15#define EXIT_ON_FALSE(_f)     \
 16    if (!(_f))    \
 17{   \
 18    hr = E_FAIL; \
 19    goto FuncExit; \
 20
 21
 22#define MAX(i, j)   ((i) > (j) ? (i) : (j))
 23
 24#define TAPI_API_LOW_VERSION   0x00020000
 25#define TAPI_API_HIGH_VERSION  0x00020000
 26
 27#define CAPS_BUFFER_SIZE    512
 28
 29 
 30
 31HRESULT SHReadLineAddressCaps(LPTSTR szNumber, UINT cchNumber, PDWORD pdwCallFwdModes, UINT nLineNumber)
 32{
 33    HRESULT  hr = E_FAIL;
 34    LRESULT  lResult = 0;
 35    HLINEAPP hLineApp;
 36    DWORD    dwNumDevs;
 37    DWORD    dwAPIVersion = TAPI_API_HIGH_VERSION;
 38    LINEINITIALIZEEXPARAMS liep;
 39
 40    DWORD dwTAPILineDeviceID;
 41    const DWORD dwAddressID = nLineNumber - 1;
 42
 43    liep.dwTotalSize = sizeof(liep);
 44    liep.dwOptions   = LINEINITIALIZEEXOPTION_USEEVENT;
 45
 46    if (SUCCEEDED(lineInitializeEx(&hLineApp, 0, 0, TEXT("ExTapi_Lib"), &dwNumDevs, &dwAPIVersion, &liep)))
 47    {
 48
 49        BYTE* pCapBuf = NULL;
 50        DWORD dwCapBufSize = CAPS_BUFFER_SIZE;
 51        LINEEXTENSIONID  LineExtensionID;
 52        LINEDEVCAPS*     pLineDevCaps = NULL;
 53        LINEADDRESSCAPS* placAddressCaps = NULL;
 54
 55        pCapBuf = new BYTE[dwCapBufSize];
 56        EXIT_ON_NULL(pCapBuf);
 57
 58        pLineDevCaps = (LINEDEVCAPS*)pCapBuf;
 59        pLineDevCaps->dwTotalSize = dwCapBufSize;
 60
 61        // Get TSP Line Device ID
 62        dwTAPILineDeviceID = 0xffffffff;
 63        for (DWORD dwCurrentDevID = 0 ; dwCurrentDevID < dwNumDevs ; dwCurrentDevID++)
 64        {
 65            if (0 == lineNegotiateAPIVersion(hLineApp, dwCurrentDevID, TAPI_API_LOW_VERSION, TAPI_API_HIGH_VERSION,
 66                &dwAPIVersion, &LineExtensionID))
 67            {
 68                lResult = lineGetDevCaps(hLineApp, dwCurrentDevID, dwAPIVersion, 0, pLineDevCaps);
 69
 70                if (dwCapBufSize < pLineDevCaps->dwNeededSize)
 71                {
 72                    delete[] pCapBuf;
 73                    dwCapBufSize = pLineDevCaps->dwNeededSize;
 74                    pCapBuf = new BYTE[dwCapBufSize];
 75                    EXIT_ON_NULL(pCapBuf);
 76
 77                    pLineDevCaps = (LINEDEVCAPS*)pCapBuf;
 78                    pLineDevCaps->dwTotalSize = dwCapBufSize;
 79
 80                    lResult = lineGetDevCaps(hLineApp, dwCurrentDevID, dwAPIVersion, 0, pLineDevCaps);
 81                }
 82
 83                if ((0 == lResult) &&
 84                    (0 == _tcscmp((TCHAR*)((BYTE*)pLineDevCaps+pLineDevCaps->dwLineNameOffset), CELLTSP_LINENAME_STRING)))
 85                {
 86                    dwTAPILineDeviceID = dwCurrentDevID;
 87                    break;
 88                }
 89            } 
 90        } 
 91
 92        placAddressCaps = (LINEADDRESSCAPS*)pCapBuf;
 93        placAddressCaps->dwTotalSize = dwCapBufSize;
 94
 95        lResult = lineGetAddressCaps(hLineApp, dwTAPILineDeviceID, dwAddressID, dwAPIVersion, 0, placAddressCaps);
 96
 97        if (dwCapBufSize < placAddressCaps->dwNeededSize)
 98        {
 99            delete[] pCapBuf;
100            dwCapBufSize = placAddressCaps->dwNeededSize;
101            pCapBuf = new BYTE[dwCapBufSize];
102            EXIT_ON_NULL(pCapBuf);
103
104            placAddressCaps = (LINEADDRESSCAPS*)pCapBuf;
105            placAddressCaps->dwTotalSize = dwCapBufSize;
106
107            lResult = lineGetAddressCaps(hLineApp, dwTAPILineDeviceID, dwAddressID, dwAPIVersion, 0, placAddressCaps);
108        }
109
110        if (0 == lResult)
111        {
112            if (szNumber)
113            {
114                szNumber[0= _T('\0');
115
116                EXIT_ON_FALSE(0 != placAddressCaps->dwAddressSize);
117
118                // A non-zero dwAddressSize means a phone number was found
119                ASSERT(0 != placAddressCaps->dwAddressOffset);
120                PWCHAR tsAddress = (WCHAR*)(((BYTE*)placAddressCaps)+placAddressCaps->dwAddressOffset);
121
122                StringCchCopy(szNumber, cchNumber, tsAddress);
123            }
124
125            // Record the allowed forwarding modes
126            if (pdwCallFwdModes)
127            {
128                *pdwCallFwdModes = placAddressCaps->dwForwardModes;
129            }
130
131            hr = S_OK;
132        } 
133
134        delete[] pCapBuf;
135    } // End if ()
136
137FuncExit:
138    lineShutdown(hLineApp);
139
140    return hr;
141}
142
143 
144
145// szNumber - Out Buffer for the phone number
146
147//cchNumber - size of sznumber in characters
148
149// nLineNumber - which phone line (1 or 2) to get the number for
150HRESULT SHGetPhoneNumber(LPTSTR szNumber, UINT cchNumber, UINT nLineNumber)
151{
152    return SHReadLineAddressCaps(szNumber, cchNumber, NULL, nLineNumber);
153}
154
155
156int WINAPI WinMain( HINSTANCE hInstance,
157     HINSTANCE hPrevInstance,
158     LPTSTR    lpCmdLine,
159     int       nCmdShow)
160{
161 TCHAR tcNum[MAX_PATH]={0};
162 SHGetPhoneNumber(tcNum,MAX_PATH,1);
163
164 MessageBox(NULL,tcNum,_T("Number"),MB_OK);
165 
166 return 0;
167}
168
169