不用MFC,如何通过WINDOWS API获取本机计算机名称和IP地址?
解决方案 »
- 下载了7-zip软件源码,但是,我怎么才能编译出7-zip软件呢
- 调用批处理指令关机无效
- unsigned short __stdcall SListen(void *)' : overloaded function differs only by
- 请问一个关于常量的问题.在线等待!!!!!!
- 100分求类似QQ的源代码,就是哪个联系人的Tencent_UserBar_Class_Ver1.0,难点在于那个滚动条也自定义。
- 请问自定义消息是怎么回事??
- 对VC开发全文检索有经验的进。高分奉送!
- SHBrowseForFolder()函数的使用
- 好主意
- 关于HID设备通信的问题,请高人指教!!
- 菜鸟求救,已得到位图文件头和位图数据,如何将其存为一个位图,在线等待,谢谢
- 联通的人脑子灌水了吗?
gethostbyaddr
查一下msdn
gethostbyname()
别忘了包含头文件和库
令需先初始化WSAStartup();
用后WSACleanup();
LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);
#include "winsock2.h"#pragma comment(lib,"ws2_32.lib") int main(int argc, char* argv[])
{
WORD wVer;
WSADATA wsaData;
int nRet; wVer = MAKEWORD( 2, 2 );
nRet = WSAStartup( wVer, &wsaData );
if( nRet != 0 )
{
return -1;
}
char szName[128];
gethostname( szName, 128);
struct hostent *pHostent;
pHostent=gethostbyname(szName); printf( "The Computer name is: %s, and ip is: %s\n", szName,
inet_ntoa(*(IN_ADDR *)pHostent->h_addr_list[0] ) );
WSACleanup( );
return 0;
}
include "Winsock2.h"
#include "afxtempl.h"
#include "Winnetwk.h"加入lib链接Project—》setting-》link-》object\libary modules 中
加入Ws2_32.lib Mpr.lib CList<CString,CString&> m_list;
m_list.RemoveAll(); CString strTemp;
struct hostent *host;
struct in_addr *ptr; // 获得IP地址
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum ); WSADATA wsaData;
//开始枚举网络资源
WSAStartup(MAKEWORD(1,1),&wsaData); if ( hEnum ) //如果句柄有效
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[2048];
// 调用WSAStartup后调用WNetEnumResource做进一步的枚举工作
WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
NetResource = (NETRESOURCE*)Buffer; char szHostName[200]; for ( unsigned int i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ )
{
if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) {
if ( NetResource->lpRemoteName )
{
CString strFullName = NetResource->lpRemoteName;
if ( 0 == strFullName.Left(2).Compare("\\\\") ) strFullName = strFullName.Right(strFullName.GetLength()-2);
//获得主机名
gethostname( szHostName, strlen( szHostName ) );
//由主机名获得跟它对应的主机信息
host = gethostbyname(strFullName);
if(host == NULL) continue;
ptr = (struct in_addr *) host->h_addr_list[0];
// 提取IP地址信息,地址形式如下: 211.40.35.76
int a = ptr->S_un.S_un_b.s_b1; // 211
int b = ptr->S_un.S_un_b.s_b2; // 40
int c = ptr->S_un.S_un_b.s_b3; // 35
int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%s --> %d.%d.%d.%d",strFullName,a,b,c,d);
// 加入到链表中
m_list.AddTail(strTemp);
}
}
}
delete Buffer;
// 结束枚举工作
WNetCloseEnum( hEnum );
} // 卸载Winsock.dll
WSACleanup(); 列出本机IP地址和名字
CString m_strIPAddress;
WSADATA wsaData;
int iRet = WSAStartup(MAKEWORD(0x02, 0x02), &wsaData);
if (iRet != 0)
{
TRACE("初始化winsock动态库出错!");
m_strIPAddress = "";
return;
} struct in_addr localaddr;
struct hostent *hp=NULL;
char hostname[50];
gethostname(hostname,49);//主机名 hp=gethostbyname(hostname);//主机信息
memcpy(&localaddr,hp->h_addr,hp->h_length);//地址 m_strIPAddress = inet_ntoa(localaddr);//变成char *
WSACleanup();