16,470
社区成员
发帖
与我相关
我的任务
分享
#include <afxinet.h>
#define REMOTE_URL L"http://120.31.131.131:26000/fla/GetIp.asp" // 这个网页会把访问IP输出
// 获取外网IP
DWORD CELConnectionBase::GetIp(LPTSTR pszBuffer /*= NULL*/, int nBufLen /*= 0*/)
{
LPSTR pchIp = NULL;
DWORD dwIp = 0;
try
{
CInternetSession session(NULL, 0);
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000);
CHttpFile *pFile = (CHttpFile *)session.OpenURL(REMOTE_URL);
int nTextLen = (int)(UINT_PTR)pFile->GetLength();
if (nTextLen > 0 && nTextLen <= 15)
{
nTextLen = (nTextLen / 32 + 1) * 32;
pchIp = new char[nTextLen];
::memset(pchIp, 0, nTextLen);
pFile->Read(pchIp, nTextLen);
dwIp = ::inet_addr(pchIp);
if (pszBuffer != NULL && nBufLen >= 16)
::MultiByteToWideChar(CP_ACP, 0, pchIp, nTextLen, pszBuffer, nBufLen);
}
pFile->Close();
delete pFile;
}
catch (CException *e)
{
#ifdef _DEBUG
TCHAR szBuffer[1024];
e->GetErrorMessage(szBuffer, 1024);
int nLen = ::lstrlen(szBuffer);
szBuffer[nLen++] = '\n';
szBuffer[nLen] = '\0';
::OutputDebugString(szBuffer);
#endif
dwIp = 0;
e->Delete();
}
if (pchIp != NULL) delete[] pchIp;
return dwIp;
}