|
![]() | 作者: itianshi [itianshi]
![]() |
登录 |
我要问的是api里那句是"返回本机ip的",因为我这资料少,所以只好找大家帮助了,声明 参数的用法,谢了。。。 |
地主 发表时间: 02/16 14:24 |
![]() | 回复: sugus2002 [sugus2002] ![]() |
登录 |
我只会用WINSOCK来返回本机的IP地址 |
B1层 发表时间: 02/17 23:10 |
![]() | 回复: haounix [haounix] ![]() |
登录 |
很多菜鸟不会啊,请竹多写点啊!!! |
B2层 发表时间: 04-07-11 11:21 |
![]() | 回复: Frankiez [frankiez84] ![]() |
登录 |
给你点代码,不过不是VB的,自己看着改吧 hostent *p; char s[128]; char *p2; //Get the computer name gethostname(s, 128); p = gethostbyname(s); //Memo1->Lines->Add(p->h_name); //Get the IpAddress p2 = inet_ntoa(*((in_addr *)p->h_addr)); Memo1->Lines->Add(p2); --------------------------------------------------------------- //Win32API: // WSAStartup // WSACleanup // gethostname // gethostbyname #include <windows.h> #include <iostream.h> void main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ MessageBox(NULL, "Wrong WinSock Version.", "Error", MB_OK); return; } const int iChkIpTimes = 26; int localIPnums = 0; char hostName[80]; LPHOSTENT lpHostEnt; struct in_addr addr[iChkIpTimes]; for ( int i=0; i<iChkIpTimes; i++) { memset(&addr[i], 0, sizeof(in_addr)); } if ( gethostname(hostName, sizeof(hostName)) == SOCKET_ERROR ) { MessageBox(NULL, "Can't getting local host name.", "Error", MB_OK); WSACleanup(); return; } cout << "Local host name:" << hostName << endl; lpHostEnt = gethostbyname( hostName ); if ( !lpHostEnt ) { MessageBox(NULL, "Bad host lookup!", "Error", MB_OK); WSACleanup(); return; } for ( int i=0; i<iChkIpTimes; i++) { if ( lpHostEnt->h_addr_list[i] == 0 ) { localIPnums = i; break; } memcpy(&addr[i], lpHostEnt->h_addr_list[i], sizeof(in_addr)); } for ( int i=0; i<localIPnums; i++) { cout << "Local IP Address " << i << " : " << inet_ntoa(addr[i]) << endl; } if ( LOBYTE( wsaData.wVersion ) != 2 ¦ ¦ HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup(); return; } } --------------------------------------------------------------- AnsiString __fastcall TComputerIPAddress::GetIPAddress() { struct hostent *thisHost; struct in_addr in; char MyName[80]; char *Ip,*ptr; WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 0 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we couldn't find a usable */ /* WinSock DLL. */ return NULL; } /* Confirm that the WinSock DLL supports 2.0.*/ /* Note that if the DLL supports versions greater */ /* than 2.0 in addition to 2.0, it will still return */ /* 2.0 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 2 ¦ ¦ HIBYTE( wsaData.wVersion ) != 0 ) { /* Tell the user that we couldn't find a usable */ /* WinSock DLL. */ WSACleanup( ); return NULL; } if(gethostname(MyName,80)==SOCKET_ERROR) return NULL; if(!(thisHost=gethostbyname(MyName))) return NULL; memset((void *)&in,sizeof(in),0); in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]); if(!(ptr=inet_ntoa(in))) return NULL; WSACleanup( ); Ip=new char[strlen(ptr)+1]; strcpy(Ip,ptr); return Ip; } --------------------------------------------------------------- |
B3层 发表时间: 04-07-12 08:21 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号