⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datacall.cpp

📁 EVC PPC2003 获取GPRS拨号的IP地址
💻 CPP
字号:
// DataCall.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

#include "winsock.h"

#if (WIN32_PLATFORM_PSPC>300 || WIN32_PLATFORM_WFSP )
#include <objbase.h>
#include <initguid.h>
#include <connmgr.h>

typedef HRESULT (*CONNMGRCONNECTIONSTATUS)(HANDLE hConnection,DWORD *pdwStatus);
typedef HRESULT (*CONNMGRRELEASECONNECTION)(HANDLE hConnection,LONG lCache );
typedef HRESULT (*CONNMGRESTABLISHCONNECTION)(CONNMGR_CONNECTIONINFO *pConnInfo, HANDLE *phConnection,DWORD dwTimeout,DWORD *pdwStatus);
#endif
HANDLE phWebConnection;

BOOL EstablishDatacall(TCHAR *IPout);


int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
 	// TODO: Place code here.
	TCHAR MyIP[20];
	if (EstablishDatacall(MyIP)==TRUE)
	{
		TCHAR Message[255];
		wsprintf(Message,L"New IP: %s",MyIP);
		MessageBox(NULL,Message,L"Success",MB_OK);
	}
	else
		MessageBox(NULL,L"Data call could not be established",L"Warning",MB_OK);
	return 0;
}


BOOL EstablishDatacall(TCHAR *IPout)
{
CHAR    szHostname[255];      
TCHAR   IP[17];
HOSTENT *pHostEnt=NULL;
int      nAdapter = 0;
IN_ADDR *tsim=NULL;
BOOL	   tried2Connect=FALSE;

    IP[0]=0;	// Clear the IP Address
	if (IPout!=NULL) IPout[0]=0;
tryagain:
	nAdapter=0;
    gethostname( szHostname, sizeof( szHostname ));
    pHostEnt = gethostbyname( szHostname );
    while ( pHostEnt!=NULL && pHostEnt->h_addr_list[nAdapter] )      
	{
        // in case a device has multiple ethernet cards
		// i.e. 802.11, Bluetooth, USB-Cradle
		// we need to go though all pHostEnt->h_addr_list[nAdapter]
		tsim=(IN_ADDR *)pHostEnt->h_addr_list[nAdapter];
		if (tsim->S_un.S_un_b.s_b1==192 || 
			tsim->S_un.S_un_b.s_b1==169 || 
			tsim->S_un.S_un_b.s_b1==127 || 
			tsim->S_un.S_un_b.s_b1==255) 
		// If you want to make sure you have a real Internet
		// connection you cannot bet on IpAddresses starting with
		// 127 and 255. 192 and 169 are local IP addresses and
		// might be routed or proxied
			nAdapter++;      
		else
		{
			wsprintf(IP,TEXT("%d.%d.%d.%d"),
                        tsim->S_un.S_un_b.s_b1,
                        tsim->S_un.S_un_b.s_b2,
                        tsim->S_un.S_un_b.s_b3,
                        tsim->S_un.S_un_b.s_b4);
			if (IPout!=NULL)
				wsprintf(IPout,IP);
			break;
		}
	}

// the next lines only work with Pocket PC Phone 
// and Smartphone
#if (WIN32_PLATFORM_PSPC>300 || WIN32_PLATFORM_WFSP )
// Pocket PC Phone Edition has set WIN32_PLATFORM_PSPC to 310
	if (IP[0]==0 && tried2Connect==FALSE)
	{
		CONNMGRCONNECTIONSTATUS	g_hConnMgrConnectionStatus = NULL;
		CONNMGRESTABLISHCONNECTION g_hConnMgrEstablishConnectionSync=NULL;
		// It is good practice to load the cellcore.dll
		// dynamically to be able to compile the code even for
		// older platforms
		HINSTANCE	hcellDll = LoadLibrary(TEXT("cellcore.dll"));
		if (hcellDll)
		{
			// We need the Status and a call to establish the 
			// connection
			g_hConnMgrConnectionStatus =(CONNMGRCONNECTIONSTATUS)GetProcAddress(hcellDll,TEXT("ConnMgrConnectionStatus")); 
			// The next line is just for debugging. You will have
			// to decide what you want to do if this call fails
			DWORD a=GetLastError();
			g_hConnMgrEstablishConnectionSync =(CONNMGRESTABLISHCONNECTION)GetProcAddress(hcellDll,TEXT("ConnMgrEstablishConnectionSync")); 
			a=GetLastError();

			// Here comes the main code:
			// First we check if we might have a connection
			DWORD  pdwStatus;
			(* g_hConnMgrConnectionStatus)(&phWebConnection,&pdwStatus);
			if (pdwStatus==CONNMGR_STATUS_CONNECTED)
			{
				//We are already connected!
				//This code should never run since we should
				//have a valid IP already.
				//If you still get here, you probably have
				//stale connection. 
			}
			else
			{
				//We are not connected, so lets try:
				//The CONNECTIONINFO is the structure that 
				//tells Connection Manager how we want
				//to connect
				CONNMGR_CONNECTIONINFO sConInfo;
				memset(&sConInfo,0, sizeof(CONNMGR_CONNECTIONINFO));
				sConInfo.cbSize=sizeof(CONNMGR_CONNECTIONINFO);
				// We want to use the 揼uidDestNet

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -