当前位置: 代码迷 >> Windows Mobile >> 请问怎么断开当前的连接点连接
  详细解决方案

请问怎么断开当前的连接点连接

热度:198   发布时间:2016-04-25 08:01:38.0
请教如何断开当前的连接点连接?
小弟自己建立网络连接点,
但是在这之前要断开当前正在使用的连接,请教各位高手,
怎样判断当前使用连接,并且断开该连接。

------解决方案--------------------
C/C++ code
/*******************************************************************@func    挂断所有拨号连接,包括自己建立的或其它程序建立的*@param    无*@ret    TRUE,动作成功;FALSE,动作失败******************************************************************/BOOL CConnectMgr::hangup(){    BOOL bRet = FALSE;    LPBYTE pBuffer = NULL;    DWORD    dwStatus;    //释放自己建立的连接    if(CConnectMgr::m_ConnHandle)    {        ConnMgrReleaseConnection(CConnectMgr::m_ConnHandle, 0);        CConnectMgr::m_ConnHandle = 0;        bRet = TRUE;    }    //枚举存在的连接方式     DWORD dwSize;    int hr = ConnMgrQueryDetailedStatus(NULL, &dwSize);    if(STRSAFE_E_INSUFFICIENT_BUFFER != hr)    {        goto errorOut;    }    pBuffer = new BYTE[dwSize];    if(NULL == pBuffer)    {        goto errorOut;    }    hr = ConnMgrQueryDetailedStatus((CONNMGR_CONNECTION_DETAILED_STATUS*)pBuffer, &dwSize);    if(S_OK == hr)    {        CONNMGR_CONNECTION_DETAILED_STATUS* cmStatus = (CONNMGR_CONNECTION_DETAILED_STATUS*)pBuffer;        while(NULL != cmStatus)        {            //找到其它程序建立的连接,占有之            if(cmStatus->dwType == CM_CONNTYPE_CELLULAR &&                    cmStatus->dwConnectionStatus == CONNMGR_STATUS_CONNECTED)            {                CONNMGR_CONNECTIONINFO ConnInfo={0};                ConnInfo.cbSize        =sizeof(ConnInfo);                ConnInfo.dwParams    =CONNMGR_PARAM_GUIDDESTNET;                ConnInfo.dwPriority    =CONNMGR_PRIORITY_USERINTERACTIVE ;                ConnInfo.bExclusive = TRUE;        //独占模式                ConnInfo.guidDestNet= cmStatus->guidDestNet;                hr = ConnMgrEstablishConnectionSync(&ConnInfo, &CConnectMgr::m_ConnHandle,                     TIMEOUT_DIAL, &dwStatus);                if(S_OK == hr)                {                     //占有之后,释放之                    ConnMgrReleaseConnection(CConnectMgr::m_ConnHandle, 0);                    bRet = TRUE;                    /*hr = ConnMgrConnectionStatus(CConnectMgr::m_ConnHandle, &dwStatus);                    if(hr == S_OK && dwStatus == CONNMGR_STATUS_CONNECTED)                    {                        bRet = TRUE;                        break;                    }*/                }                else                {                    bRet = FALSE;                    goto errorOut;                }            }            cmStatus = cmStatus->pNext;        }    }    errorOut:    if(pBuffer)    delete[] pBuffer;    return bRet;}
------解决方案--------------------
hangup断开方式如下:
C# code
 private void Disconnection()  //断开网络        {            RASManager.RASCONN[] conns = RASManager.GetRASConnections();            if (conns != null && conns.Length > 0)            {                foreach (RASManager.RASCONN conn in conns)                {                    try                    {                        RASManager.HangUp(conn.hRasConnHandle);                    }                    catch (Exception ex)                    {                        Log.Write("ChangeNetConnection #5 断开网络失败 " + ex.Message + "\r\n" + ex.StackTrace);                        throw new Exception(ex.Message);                    }                }            }        } public static RASCONN[] GetRASConnections()        {            RASCONN[] retval = null;            int size = Marshal.SizeOf(typeof(RASCONN));            int cb = size;            int iConnections = 0;            IntPtr pConnections = IntPtr.Zero;            bool retry = false;            do            {                try                {                    if (pConnections != IntPtr.Zero)                    {                        try                        {                            Marshal.FreeHGlobal(pConnections);                        }                        catch                        { }                    }                    pConnections = Marshal.AllocHGlobal(cb);                    Marshal.Copy(BitConverter.GetBytes(size), 0, pConnections, 4); //pConnections->size = size;                    int ret = RasEnumConnections(pConnections, ref cb, ref iConnections);                    if (ret != 0)  //ERROR_BUFFER_TOO_SMALL                    {                        retry = true;                    }                    else if(ret==0)                    {                        retry = false;                        if (iConnections > 0)                        {                            retval = CreateArrayOfType<RASCONN>(pConnections, size, iConnections);                        }                    }                     else                    {                        throw new Exception(string.Format("RasEnumConnections failed: Error {0}", ret));                    }                }                finally                {                    if (pConnections != IntPtr.Zero)                    {                        Marshal.FreeHGlobal(pConnections);                    }                }            } while (retry);            return retval;        }public static void HangUp(IntPtr hSession)        {            int lStatus = RasHangUp(hSession);            if (lStatus != 0)            {                throw new Exception(string.Format("RasHangUp failed: Error {0}", lStatus));            }        }
  相关解决方案