// GPRSwitch.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "GPRSwitch.h" #include <windows.h> #include <commctrl.h> #include "GprsXmlConfig.h" #include <connmgr.h> #include <ras.h> #include <Raserror.h> //#include <stdio.h> #define MAX_LOADSTRING 100 #define WM_GPRSwitch WM_USER+7227 #define WM_CTLCENTER WM_USER+112 #define GPRS_STATUS_ON 1 #define GPRS_STATUS_OFF 0 // 全局变量: HINSTANCE g_hInst; // 当前实例 HANDLE hConnection; HRESULT hResult; DWORD dwStatus; GprsXmlConfig config; DWORD bufsize; DWORD numEntries; //FILE *fp; HWND hWndDes = NULL; // 接收消息的主程序句柄 // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE, LPTSTR); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; nCmdShow = SW_HIDE; // 执行应用程序初始化: if (!InitInstance(hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable; hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GPRSWITCH)); // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // // 注释: // ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass) { WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GPRSWITCH)); wc.hCursor = 0; wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = szWindowClass; return RegisterClass(&wc); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; TCHAR szTitle[MAX_LOADSTRING] = _T("GPRS Switcher"); // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING] = _T("TBWidget"); // 主窗口类名 g_hInst = hInstance; // 将实例句柄存储在全局变量中 SHInitExtraControls(); LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_GPRSWITCH, szWindowClass, MAX_LOADSTRING); hWnd = FindWindow(szWindowClass, szTitle); if (hWnd) { return 0; } if (!MyRegisterClass(hInstance, szWindowClass)) { return FALSE; } hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); hWndDes = FindWindow(_T("TBClass"), _T("TBMainWnd")); SendMessage(hWnd, WM_GPRSwitch, 0, 0); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_GPRSwitch: RASCONN entries[25]; bufsize = 0; numEntries = 0; bufsize = 25 * sizeof(RASCONN); entries[0].dwSize = sizeof(RASCONN);//必须 RASCONNSTATUS status;//RAS状态在ras.h中可以查找到。 status.dwSize = sizeof(RASCONNSTATUS); //fp = fopen("GPRSwitch.log", "w"); if(RasEnumConnections(entries, &bufsize, &numEntries) == 0) //枚举所有活动的连接 { if (numEntries == 0) //无GPRS连接 { RASDIALPARAMS rdParams; HRASCONN hRasConn = NULL; //初始化参数 rdParams.dwSize = sizeof(RASDIALPARAMS); lstrcpy(rdParams.szEntryName, _T("GPRS")); lstrcpy(rdParams.szPhoneNumber, _T("")); lstrcpy(rdParams.szCallbackNumber, _T("")); lstrcpy(rdParams.szPhoneNumber, _T("")); lstrcpy(rdParams.szUserName, _T("")); lstrcpy(rdParams.szPassword, _T("")); lstrcpy(rdParams.szDomain, _T("")); DWORD dwRet; dwRet = RasDial(NULL, NULL, &rdParams, 0xFFFFFFFF, NULL, &hRasConn); //通过给窗体发送WM_RASDIALEVENT消息。 if(dwRet != 0) //拔号失败 { if (hWndDes != NULL) { SendMessage(hWndDes, WM_CTLCENTER, WM_GPRSwitch, GPRS_STATUS_OFF); } } else { if (hWndDes != NULL) { SendMessage(hWndDes, WM_CTLCENTER, WM_GPRSwitch, GPRS_STATUS_ON); } } exit(0); break; } for(DWORD i=0; i<numEntries; i++) // 有GPRS连接 { for (DWORD j=0; j<wcslen(entries[i].szEntryName)-4; j++) { if (entries[i].szEntryName[j] == 'G' && entries[i].szEntryName[j+1] == 'P' && entries[i].szEntryName[j+2] == 'R' && entries[i].szEntryName[j+3] == 'S' ) { RasHangUp(entries[i].hrasconn); while(RasGetConnectStatus(entries[i].hrasconn,&status)==0)//得到连接的状态(能获取得到) { Sleep(0); //Sleep(0)是指CPU交出当前线程的执行权,让CPU去执行其他线程。 } break; } } //fwprintf(fp, _T("%d : %s\n"), i, entries[i].szEntryName); } if (hWndDes != NULL) { SendMessage(hWndDes, WM_CTLCENTER, WM_GPRSwitch, GPRS_STATUS_OFF); } } //fclose(fp); exit(0); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_CLOSE: exit(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
// GPRSXmlConfig.h #pragma once /*! * 通过wap-provisioningdoc方式设置gprs连接 * 使用方式:枚举网络找一个名为CMWAP_GPRS的连接,(如果找不到新建一个名为CMWAP_GPRS的网络连接), * 然后通过建立该连接来进行网络访问 */ class GprsXmlConfig { public: GprsXmlConfig(void); ~GprsXmlConfig(void); public: /* 创建一个名为CMWAP_GPRS的网络设置*/ BOOL CreateCmwapNetConfig(); /*选择名为CMWAP_GPRS的网络设置的GUID*/ BOOL findCmwapNetGuid(GUID&); };
// GprsXmlConfig.cpp #include "StdAfx.h" #include "GprsXmlConfig.h" #include <Cfgmgrapi.h> #include <connmgr.h> #include <connmgr_proxy.h> #pragma comment( lib,"cellcore.lib") #pragma comment(lib, "Aygshell.lib") GprsXmlConfig::GprsXmlConfig(void) { } GprsXmlConfig::~GprsXmlConfig(void) { } BOOL GprsXmlConfig::findCmwapNetGuid(GUID& guid) { BOOL rst = false; HRESULT hr = 0; int nIndex = 0; CONNMGR_DESTINATION_INFO DestInfo; //WCHAR wszDestGUIDStr[128]; while ( (hr = ConnMgrEnumDestinations(nIndex,&DestInfo)) == 0 ) { if (_tcscmp(_T("中国移动WAP设置"),DestInfo.szDescription)==0) { guid = DestInfo.guid; rst = true; break; } nIndex++; } return rst; } BOOL GprsXmlConfig::CreateCmwapNetConfig() { //AfxMessageBox(_T("开始创建连接")); LPCWSTR g_wszFavoriteXml = L"<wap-provisioningdoc>" L" <characteristic type=\"CM_Networks\">" L" <characteristic type=\"中国移动WAP设置\">" L" <parm name=\"DestId\" value=\"{EA9A391E-7878-0D28-4CA2-21F8C61F1243}\"/>" L" </characteristic>" L" </characteristic>" L" <characteristic type=\"CM_GPRSEntries\">" L" <characteristic type=\"中国移动 GPRS 因特网\">" L" <parm name=\"DestId\" value=\"{EA9A391E-7878-0D28-4CA2-21F8C61F1243}\"/>" L" <characteristic type=\"DevSpecificCellular\">" L" <parm name=\"BearerInfoValid\" value=\"1\"/>" L" <parm name=\"GPRSInfoValid\" value=\"1\"/>" L" <parm name=\"GPRSInfoProtocolType\" value=\"2\"/>" L" <parm name=\"GPRSInfoL2ProtocolType\" value=\"PPP\"/>" L" <parm name=\"GPRSInfoAccessPointName\" value=\"cmwap\"/>" L" <parm name=\"GPRSInfoAddress\" value=\"\"/>" L" <parm name=\"GPRSInfoDataCompression\" value=\"1\"/>" L" <parm name=\"GPRSInfoHeaderCompression\" value=\"1\"/>" L" <parm name=\"GPRSInfoParameters\" value=\"\"/>" L" </characteristic>" L" </characteristic>" L" </characteristic>" L" <characteristic type=\"CM_ProxyEntries\">" L" <characteristic type=\"HTTP-{EA9A391E-7878-0D28-4CA2-21F8C61F1243}\">" L" <parm name=\"SrcId\" value=\"{EA9A391E-7878-0D28-4CA2-21F8C61F1243}\"/>" L" <parm name=\"DestId\" value=\"{436EF144-B4FB-4863-A041-8F905A62C572}\"/>" L" <parm name=\"Proxy\" value=\"10.0.0.172:80\"/>" L" <parm name=\"Enable\" value=\"1\"/>" L" <parm name=\"Type\" value=\"1\"/>" L" </characteristic>" L" <characteristic type=\"WAP-{EA9A391E-7878-0D28-4CA2-21F8C61F1243}\">" L" <parm name=\"SrcId\" value=\"{EA9A391E-7878-0D28-4CA2-21F8C61F1243}\"/>" L" <parm name=\"DestId\" value=\"{7022E968-5A97-4051-BC1C-C578E2FBA5D9}\"/>" L" <parm name=\"Proxy\" value=\"10.0.0.172:9201\"/>" L" <parm name=\"Enable\" value=\"1\"/>" L" <parm name=\"Type\" value=\"2\"/>" L" </characteristic>" L" </characteristic>" L"</wap-provisioningdoc>"; HRESULT hr = E_FAIL; LPWSTR wszOutput = NULL; hr = DMProcessConfigXML(g_wszFavoriteXml, CFGFLAG_PROCESS, &wszOutput); // if (hr==S_OK) // { // AfxMessageBox(_T("创建连接成功!")); // } if (wszOutput) { delete [] wszOutput; wszOutput=NULL; } return (hr==S_OK); }