본문 바로가기

Windows/MFC

GPRS connection

[code]http://www.pcreview.co.uk/forums/thread-1306016.php

#include <windows.h>
//#include <commctrl.h>
//#include <aygshell.h>
//#include <sipapi.h>

#include <connmgr.h>
#include <connmgr_proxy.h>
#include <ras.h>
#include <raserror.h>

#define RNAAPP_MAGIC_NUM 0x006A6D6D
#define RNA_GETINFO 3


#define GPRS_RETRY_COUNT 5


#ifdef __cplusplus
extern "C" {
#endif


    __declspec(dllexport) BOOL InitializeConnectionInfo(TCHAR *tchNetworkDescription,HWND hWnd,UINT uMsg = WM_USER);
    __declspec(dllexport) BOOL EstablishSynchronousGPRSConnection(HANDLE *phConnection,DWORD * pdwStatus,BOOL *pinAvailable);
    __declspec(dllexport) BOOL GetSynchronousGPRSConnectionStatus(HANDLE *phConnection,DWORD * pdwStatus,BOOL *pinAvailable);
    __declspec(dllexport) BOOL GetGPRSConnectionStatus(HANDLE hConnection,DWORD * pdwStatus);
    __declspec(dllexport) BOOL CloseGPRSConnection(HANDLE phConnection);

#ifdef __cplusplus
}

#endif



HANDLE hConnMgr = NULL;
GUID guidNetworkObject;
CONNMGR_CONNECTIONINFO ccInfo;
DWORD dwStatus = 0;
DWORD dwTimeout = 360000;



BOOL InitializeConnectionInfo(TCHAR *tchNetworkDescription,HWND hWnd,UINT
                             uMsg)
{
    CONNMGR_DESTINATION_INFO networkDestInfo;
    DWORD dwEnumIndex = 0;
    BOOL fLoop = TRUE;
    HANDLE hConnMgr = NULL;
    BOOL bAvailable = FALSE;

    hConnMgr = ConnMgrApiReadyEvent();

    if(!hConnMgr)
        return FALSE;

    // Wait for 1 second to see if Connection Manager
    // has signaled the event
    DWORD dwResult = WaitForSingleObject(hConnMgr, 1000);

    if(dwResult == WAIT_OBJECT_0)
        bAvailable = TRUE;

    // Close the handle
    if(hConnMgr)
        CloseHandle(hConnMgr);

    // Did it connect ok?
    if(!bAvailable)
        return FALSE;

    // Walk through the list of Networks
    do{
        memset(&networkDestInfo, 0, sizeof(CONNMGR_DESTINATION_INFO));
        if(ConnMgrEnumDestinations(dwEnumIndex, &networkDestInfo) == E_FAIL) {
            fLoop = FALSE;
            break;
        }
        if (!wcscmp(networkDestInfo.szDescription,tchNetworkDescription )){
            guidNetworkObject = networkDestInfo.guid;
            break;
        }
        dwEnumIndex++;
    }while(fLoop);

    if(fLoop)
    {
        memset(&ccInfo, 0, sizeof(CONNMGR_CONNECTIONINFO));
        ccInfo.cbSize = sizeof(CONNMGR_CONNECTIONINFO);
        ccInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;//CONNMGR_PARAM_MAXCOST;//CONNMGR_PARAM_GUIDDESTNET;
        ccInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
        //CONNMGR_PRIORITY_USERIDLE;
        /*CONNMGR_PRIORITY_USERIDLE;*//*CONNMGR_PRIORITY_HIPRIBKGND,*//*CONNMGR_PRIO
                                                                     RITY_USERINTERACTIVE;*/
        ccInfo.bExclusive = FALSE;
        ccInfo.bDisabled = false;
        ccInfo.guidDestNet = guidNetworkObject;
        ccInfo.hWnd = hWnd;
        ccInfo.uMsg = uMsg;//WM_USER_CONNECTED;
        ccInfo.ulMaxCost = CONNMGR_PARAM_MAXCOST;
        ccInfo.ulMinRcvBw = CONNMGR_PARAM_MINRCVBW;
        ccInfo.ulMaxConnLatency = CONNMGR_PARAM_MAXCONNLATENCY;
    }

    return fLoop;

}

BOOL CloseGPRSConnection(HANDLE phConnection)
{
    //if(phConnection) {
    if(ConnMgrReleaseConnection(phConnection, TRUE) == S_OK)
    {
        phConnection = NULL;
        dwStatus = 0;
        hConnMgr = 0;
        return TRUE;
    }

    // }
    return FALSE;
}

DWORD CloseRasGPRSConnections ()
{
    int index; // An integer index
    TCHAR szError[100]; // Buffer for error codes
    DWORD dwError, // Error code from a function call
        dwRasConnSize, // Size of RasConn in bytes
        dwNumConnections; // Number of connections found
    RASCONN RasConn[20]; // Buffer for connection state data
    // Assume the maximum number of entries is
    // 20.

    // Assume no more than 20 connections.
    RasConn[0].dwSize = sizeof (RASCONN);
    dwRasConnSize = 20 * sizeof (RASCONN);

    // Find all connections.
    if (dwError = RasEnumConnections (RasConn, &dwRasConnSize,
        &dwNumConnections))
    {
        wsprintf (szError, TEXT("RasEnumConnections Error: %ld"), dwError);
        return dwError;
    }

    // If there are no connections, return zero.
    if (!dwNumConnections)
    {
        wsprintf (szError, TEXT("No open RAS connections"));
        return 0;
    }

    // Terminate all of the remote access connections.
    for (index = 0; index < (int)dwNumConnections; ++index)
    {
        wsprintf (szError, TEXT("Ras connectin get"), dwError);
        if (dwError = RasHangUp (RasConn[index].hrasconn))
        {
            wsprintf (szError, TEXT("RasHangUp Error: %ld"), dwError);
            return dwError;
        }
    }

    return 0;
}

BOOL EstablishSynchronousGPRSConnection(HANDLE * phConnection,DWORD *
                                        pdwStatus, BOOL *pinAvailable)
{
    int iGPRSRetryCnt = 0; //Count for retry for GPRS connections
    hConnMgr = 0;
    dwStatus = 0;

    TCHAR st[120] = TEXT("\0");

    do{
        CloseRasGPRSConnections();
        if(FAILED(ConnMgrEstablishConnectionSync(&ccInfo, &hConnMgr, dwTimeout,
            &dwStatus)))
        {
            hConnMgr = 0;
            *phConnection = 0;
            dwStatus = 0;
            *pdwStatus = 0;
            *pinAvailable = FALSE;
            return FALSE;
        }

        iGPRSRetryCnt++;

        if((int)hConnMgr <= 0)
        {
            if(CloseGPRSConnection(hConnMgr))
            {
                hConnMgr = 0;
                *phConnection = 0;
                dwStatus = 0;
                *pdwStatus = 0;
                CloseGPRSConnection(*phConnection);
            }
        }
        if((hConnMgr <= 0) && (iGPRSRetryCnt < GPRS_RETRY_COUNT))
        {
            Sleep(5000);
        }
    }while((hConnMgr <= 0) && (iGPRSRetryCnt < GPRS_RETRY_COUNT));

    *pdwStatus = dwStatus;
    *phConnection = hConnMgr;

    wsprintf(st,_T("%d"), *phConnection);
    ::MessageBox(NULL, st, TEXT("GetGPRSConnection - Handle"), MB_OK);

    return TRUE;
}



BOOL GetGPRSConnectionStatus(HANDLE phConnection,DWORD * pdwStatus)
{
    TCHAR st[120] = TEXT("\0");
    wsprintf(st,_T("%d"),phConnection);
    ::MessageBox(NULL, st, TEXT("Before : GetGPRSConnectionStatus - Handle"),
        MB_OK);

    if(FAILED(ConnMgrConnectionStatus(phConnection,pdwStatus)))
    {
        ::MessageBox(NULL, TEXT("GetGPRSConnectionStatus - status failed"),
            TEXT("GetGPRSConnectionStatus"), MB_OK);

        return FALSE;
    }

    wsprintf(st,_T("%d"),*pdwStatus);
    ::MessageBox(NULL, st, TEXT("GetGPRSConnectionStatus - Status"), MB_OK);
    return TRUE;
}


BOOL WINAPI DllMain(HANDLE hinstDll,DWORD dwReason,LPVOID lpvReserved)
{
    return TRUE;
}
[/code]

'Windows > MFC' 카테고리의 다른 글

Using Connection Manager to Establish Data Calls  (0) 2013.10.02
GPRS Conection via Proxy  (0) 2013.10.02
Cellular Emulator사용하기  (0) 2013.10.02
SMS Send  (0) 2013.10.02
email send  (0) 2013.10.02