c++builder 2010 怎样获取本机的外网IP ? 本机连在了交换机局域网下。
C++Builder6的代码如下:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "urlmon.h" #include "stdio.h" #pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\urlmon.lib"//此处根据你的实际情况修改 //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); { char buf[MAX_PATH] = {0}; char chTempIp[128]; char chIP[64]; URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","IPinTheWorld.ini",0,NULL); FILE *fp=fopen( "IPinTheWorld.ini", "r" ); if ( fp != NULL ) { fseek(fp, 0, SEEK_SET); fread(buf,1,256,fp); fclose(fp); char* iIndex = strstr(buf,"["); if (iIndex) { sprintf( chTempIp,"%s",iIndex); int nBuflen = strlen(chTempIp); for (int i =0; i<nBuflen; i++) { chIP[i] = chTempIp[i+1]; if (chTempIp[i] == ']') { chIP[i-1] = '\0'; DeleteFile("IPinTheWorld.ini"); continue; } } } } MessageBox(NULL, chIP,chIP,0); } } //---------------------------------------------------------------------------
参考:http://blog.csdn.net/hacksouls/article/details/3511762