如何能够得到主机名称? 用什么函数能够得到主机名呢?还有一个小问题,现在登录的用户名怎么取得呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 专门为PB,VB程序获取本机IP而制作的动态库(2008.06.02更新)http://www.cnblogs.com/SummerHeart/archive/2008/05/17/1201330.html int gethostname(char *name, size_t len)这个函数,调用后,会将主机名保存在name里面。而len是name的大小。以下是例程,编译后只需要运行就知道自己的主机名字了。知道自己名字后,我再调用了一下gethostbyname()来得到主机的一些其他信息。#include <netdb.h>#include <sys/socket.h>int main(int argc, char **argv){ struct hostent *hptr; char **pptr; char hostname[32]; char str[32]; if( gethostname(hostname,sizeof(hostname)) ) { printf("gethostname calling error\n"); return 1; } printf("localhost name:%s\n",hostname); if( (hptr = gethostbyname(hostname)) == NULL) { printf("gethostbyname calling error\n"); return 1; } pptr=hptr->h_addr_list; for(;*pptr!=NULL;pptr++) printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str))); return 0;} int DllExport __stdcall Gethost(char *name){ WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return -1; } char szhn[256]; int nStatus = gethostname(szhn, sizeof(szhn)); if (nStatus == SOCKET_ERROR ) { return -1; } WSACleanup(); strcpy(name,szhn) ; return 0;} 调用了这个函数后,程序说找不到<netdb.h> 和<sys/socket.h> ,这两个头文件不存在 调用后,会报出很多的错,是不是需要头文件呢?MFC中有什么方法可以得到主机名称呢?谢谢答复! 哦,解决了。原来是需要 Winsock2.h 这个头文件。谢谢大家散分! CListView create函数问题 如何在对话框中生成工具条ToolBar 一个初学者在VC下的DLL编程的问题? 在ATL中编写无窗口控件,怎么使用定时器,各位高手多多指点 用DAO数据库怎样实现ACCESS兼容? 那里有<<Windows核心编程>>中文版下载? CEditView不能动态的修改式样吗? 《windows程序设计》其中有误???? win 2k下如何把程序注册成服务器进程? Com问题!分少,很sorry! 如何双击一对话框将值传递给另一对话框! 对话框程序中如何使菜单启用/禁用?
#include <sys/socket.h>int main(int argc, char **argv)
{
struct hostent *hptr;
char **pptr;
char hostname[32];
char str[32];
if( gethostname(hostname,sizeof(hostname)) )
{
printf("gethostname calling error\n");
return 1;
}
printf("localhost name:%s\n",hostname);
if( (hptr = gethostbyname(hostname)) == NULL)
{
printf("gethostbyname calling error\n");
return 1;
}
pptr=hptr->h_addr_list;
for(;*pptr!=NULL;pptr++)
printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
return 0;
}
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
char szhn[256];
int nStatus = gethostname(szhn, sizeof(szhn));
if (nStatus == SOCKET_ERROR )
{
return -1;
}
WSACleanup();
strcpy(name,szhn) ;
return 0;
}
谢谢答复!
原来是需要 Winsock2.h 这个头文件。
谢谢大家
散分!