//////////////////////////////////////////////////////////////////////////////////////<br />// chengchenz@mail.csdn.net<br />//<br />// GetAPN.cpp : Defines the entry point for the application.<br />//<br />//////////////////////////////////////////////////////////////////////////////////////</p><p>#include <stdlib.h><br />#include <windows.h></p><p>#include <connmgr.h><br />#include <Connmgr_status.h><br />#include <Cfgmgrapi.h><br />#include <strsafe.h></p><p>//<br />// FUNCTION: GetAPNFromEntryName(LPCTSTR szEntryName, LPTSTR szAPN, int cchAPN)<br />//<br />// PURPOSE: Get the GPRS Access Point Name form the Entry Name<br />//<br />HRESULT GetAPNFromEntryName(LPCTSTR szEntryName, LPTSTR szAPN, int cchAPN)<br />{<br /> // parm query formating string of "CM_GPRSEntries Configuration Service Provider"<br /> LPCTSTR szFormat = TEXT("<wap-provisioningdoc>")<br /> TEXT(" <characteristic type=/"CM_GPRSEntries/">")<br /> TEXT(" <characteristic type=/"%s/">")<br /> TEXT(" <characteristic type=/"DevSpecificCellular/">")<br /> TEXT(" <parm-query name=/"GPRSInfoAccessPointName/"/>")<br /> TEXT(" </characteristic>")<br /> TEXT(" </characteristic>")<br /> TEXT(" </characteristic>")<br /> TEXT("</wap-provisioningdoc>");<br /> HRESULT hr = E_FAIL;<br /> LPTSTR szOutput = NULL;</p><p> if(NULL == szEntryName)<br /> return E_INVALIDARG;</p><p> // prepare the query string with the special entry name<br /> LPTSTR szInput = new TCHAR[_tcslen(szFormat) + _tcslen(szEntryName) + 10];<br /> if(NULL == hr)<br /> return E_OUTOFMEMORY;</p><p> _stprintf(szInput, szFormat, szEntryName);</p><p> // Process the XML.<br /> hr = DMProcessConfigXML(szInput, CFGFLAG_PROCESS, &szOutput);</p><p> if(S_OK == hr)<br /> {<br /> hr = E_FAIL;</p><p> // find the value of GPRSInfoAccessPointName param<br /> LPTSTR szAPNStrStart = _tcsstr(szOutput, TEXT("value=/""));<br /> if(NULL != szAPNStrStart)<br /> {<br /> szAPNStrStart += _tcslen(TEXT("value=/""));</p><p> // find the end of value string<br /> LPTSTR szAPNStrEnd = _tcsstr(szAPNStrStart, TEXT("/""));<br /> if(NULL != szAPNStrEnd)<br /> {<br /> // set the null char at the end of the value string<br /> *szAPNStrEnd = TEXT('/0');</p><p> // get the final Access Point Name string<br /> _tcsncpy(szAPN, szAPNStrStart, cchAPN);<br /> hr = S_OK;<br /> }<br /> }</p><p> }</p><p> // the caller must delete the XML returned from DMProcessConfigXML.<br /> delete[] szOutput;</p><p> // clear the input string<br /> delete[] szInput;</p><p> return hr;<br />}</p><p>//<br />// we will enum the all connected GPRS<br />// and display their entry name and APN on the message box<br />//<br />int WINAPI WinMain(HINSTANCE hInstance,<br /> HINSTANCE hPrevInstance,<br /> LPTSTR lpCmdLine,<br /> int nCmdShow)<br />{<br /> TCHAR szAPN[200];</p><p> DWORD dwSize = 0;<br /> HRESULT hr = E_FAIL;</p><p> //<br /> // Get the the required size of the buffer<br /> // with which the function needs to be called on the next attempt.<br /> //<br /> hr = ConnMgrQueryDetailedStatus(NULL, &dwSize);<br /> if(STRSAFE_E_INSUFFICIENT_BUFFER != hr)<br /> return hr;</p><p> LPBYTE pBuffer = new BYTE[dwSize];<br /> if(NULL == pBuffer)<br /> return E_OUTOFMEMORY;</p><p> //<br /> // Get the connection information<br /> //<br /> hr = ConnMgrQueryDetailedStatus((CONNMGR_CONNECTION_DETAILED_STATUS*)pBuffer, &dwSize);<br /> if(S_OK == hr)<br /> {</p><p> //<br /> // Enum each connection entry<br /> //<br /> CONNMGR_CONNECTION_DETAILED_STATUS* cmStatus = (CONNMGR_CONNECTION_DETAILED_STATUS*)pBuffer;<br /> while(NULL != cmStatus)<br /> {<br /> // find the connected GPRS entry<br /> if((cmStatus->dwParams & (CONNMGRDETAILEDSTATUS_PARAM_TYPE |CONNMGRDETAILEDSTATUS_PARAM_DESCRIPTION | CONNMGRDETAILEDSTATUS_PARAM_CONNSTATUS)) &&CM_CONNTYPE_CELLULAR == cmStatus->dwType &&CONNMGR_STATUS_CONNECTED == cmStatus->dwConnectionStatus && NULL != cmStatus->szDescription)<br /> {<br /> // get the connected GPRS APN<br /> if(S_OK == GetAPNFromEntryName(cmStatus->szDescription, szAPN, 200))<br /> MessageBox(NULL, szAPN, cmStatus->szDescription, MB_OK | MB_ICONINFORMATION);</p><p> }</p><p> // test the next one<br /> cmStatus = cmStatus->pNext;<br /> }<br /> }</p><p> // clear the buffer<br /> delete pBuffer;</p><p> return 0;<br />}<br />
備忘:#include <Connmgr_status.h>要在wince 4.2以上版本才支援