Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 386, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

如何判断GPRS的接入点

思路:用ConnMgrQueryDetailedStatus找到Dest GUID, 然后根据Dest GUID用DMProcessConfigXML函数透过CM_GPRSEntries Configuration Service Provider来查接入点名称。
示例代码:


#include <stdlib.h>
#include <windows.h>

#include <connmgr.h>
#include <Connmgr_status.h>
#include <Cfgmgrapi.h>
#include <strsafe.h>

//
//   FUNCTION: GetAPNFromEntryName(LPCTSTR szEntryName, LPTSTR szAPN, int cchAPN)
//
//   PURPOSE: Get the GPRS Access Point Name form the Entry Name
//
HRESULT GetAPNFromEntryName(LPCTSTR szEntryName, LPTSTR szAPN, int cchAPN)
{
    // parm query formating string of "CM_GPRSEntries Configuration Service Provider"
    LPCTSTR szFormat =    TEXT("<wap-provisioningdoc>")
                        TEXT("    <characteristic type=\"CM_GPRSEntries\">")
                        TEXT("        <characteristic type=\"%s\">")
                        TEXT("            <characteristic type=\"DevSpecificCellular\">")
                        TEXT("                <parm-query name=\"GPRSInfoAccessPointName\"/>")
                        TEXT("            </characteristic>")
                        TEXT("        </characteristic>")
                        TEXT("    </characteristic>")
                        TEXT("</wap-provisioningdoc>");
    HRESULT hr = E_FAIL;
    LPTSTR szOutput   = NULL;


    if(NULL == szEntryName)
        return E_INVALIDARG;


    // prepare the query string with the special entry name
    LPTSTR szInput = new TCHAR[_tcslen(szFormat) + _tcslen(szEntryName) + 10];
    if(NULL == hr)
        return E_OUTOFMEMORY;

    _stprintf(szInput, szFormat, szEntryName);
   

    // Process the XML.
    hr = DMProcessConfigXML(szInput, CFGFLAG_PROCESS, &szOutput);

    if(S_OK == hr)
    {
        hr = E_FAIL;

        // find the value of GPRSInfoAccessPointName param
        LPTSTR szAPNStrStart = _tcsstr(szOutput, TEXT("value=\""));
        if(NULL != szAPNStrStart)
        {
            szAPNStrStart += _tcslen(TEXT("value=\""));

            // find the end of value string
            LPTSTR szAPNStrEnd = _tcsstr(szAPNStrStart, TEXT("\""));
            if(NULL != szAPNStrEnd)
            {
                // set the null char at the end of the value string
                *szAPNStrEnd = TEXT('\0');

                // get the final Access Point Name string
                _tcsncpy(szAPN, szAPNStrStart, cchAPN);
                hr = S_OK;
            }
        }

    }

    // the caller must delete the XML returned from DMProcessConfigXML.
    delete[] szOutput;

    // clear the input string
    delete[] szInput;

    return hr;
}


//
// we will enum the all connected GPRS
// and display their entry name and APN on the message box
//
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
    TCHAR szAPN[200];

    DWORD dwSize = 0;
    HRESULT hr = E_FAIL;

    //
    // Get the the required size of the buffer
    // with which the function needs to be called on the next attempt.
    //
    hr = ConnMgrQueryDetailedStatus(NULL, &dwSize);
    if(STRSAFE_E_INSUFFICIENT_BUFFER != hr)
        return hr;

    LPBYTE pBuffer = new BYTE[dwSize];
    if(NULL == pBuffer)
        return E_OUTOFMEMORY;

    //
    // Get the connection information
    //
    hr = ConnMgrQueryDetailedStatus((CONNMGR_CONNECTION_DETAILED_STATUS*)pBuffer, &dwSize);
    if(S_OK == hr)
    {

        //
        // Enum each connection entry
        //
        CONNMGR_CONNECTION_DETAILED_STATUS* cmStatus  = (CONNMGR_CONNECTION_DETAILED_STATUS*)pBuffer;
        while(NULL != cmStatus)
        {
            // find the connected GPRS entry
            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)       
            {
                // get the connected GPRS APN
                if(S_OK == GetAPNFromEntryName(cmStatus->szDescription, szAPN, 200))
                    MessageBox(NULL, szAPN, cmStatus->szDescription, MB_OK | MB_ICONINFORMATION);

            }

            // test the next one
            cmStatus = cmStatus->pNext;
        }
    }


    // clear the buffer
    delete pBuffer;


    return 0;
}

posted on 2009-02-09 17:09 Benjamin 阅读(1715) 评论(3)  编辑 收藏 引用 所属分类: PDA/PPC开发

评论

# re: 如何判断GPRS的接入点  回复  更多评论   


Cannot open include file: 'Connmgr_status.h': No such file or directory

楼主,这个问题问题怎么解决啊!
2009-07-14 10:06 | beautymind

# re: 如何判断GPRS的接入点  回复  更多评论   

用了楼主的方法,想判断 CDMA 手机是用了 CTWAP 还是 CTNET 网络,能得到网络的描述名称,但是当我进一步想拿到 APN 值时,返回的 XML 字符串是类似以下的内容:
----------------
<wap-provisioningdoc>

<characteristic type="CM_GPRSEntries">
<characteristic-error type="ctwap">
<characteristic type="DevSpecificCellular">

<parm-query name="GPRSInfoAccessPointName"/>

</characteristic>

</characteristic-error>
</characteristic></wap-provisioningdoc>
-------------------
这是什么原因呢?
2010-04-03 15:45 | Water Lin

# re: 如何判断GPRS的接入点  回复  更多评论   

GPRS和CDMA是两种不同的网络,而且CDMA的芯片是美国高通所拥有,所以不是这么判断的。一般在C网下不用判断的,都是CMWAP。
2010-04-04 08:00 | Benjamin

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理