获取主机名称和ip,用户名
2017-10-30 20:25
288 查看
#include <iostream> #include <string> #include <winsock2.h> #pragma comment(lib,"ws2_32.lib") using namespace std; int main(int argc, char* argv[]) { string ip_list; char PCnameBuffer[128]; //获得本地计算机名 WSAData data; //初始化:如果不初始化,以下代码将无法执行 if(WSAStartup(MAKEWORD(1,1),&data)!=0) { cout<<"初始化错误,无法获取主机信息..."<<endl; } else { if(0==gethostname(PCnameBuffer,128)) { struct hostent* pHost; //获得本地IP地址 pHost=gethostbyname(PCnameBuffer); //pHost返回的是指向主机的列表 for (int i=0;pHost!=NULL&&pHost->h_addr_list[i]!=NULL;i++) { string tem = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]); ip_list += tem; ip_list += "\n"; } } else { cout<<"获取主机信息失败..."<<endl ; } } cout<<PCnameBuffer<<endl; cout<<ip_list<<endl; getchar(); return 0; }
获取用户名:
#include <iostream>
#include <afx.h>
#pragma comment(lib,"Advapi32.lib")
using namespace std;
int main()
{
CString strUserName;
LPTSTR szBuffer=new wchar_t[300];
DWORD dwSize=300;
GetUserName(szBuffer,&dwSize);
strUserName=szBuffer;
delete szBuffer;
const size_t newsizew = (strUserName.GetLength() + 1)*2; //
char *ch = new char[newsizew];
WideCharToMultiByte(CP_OEMCP,NULL,strUserName,-1,ch,newsizew,0,NULL);// m_cstr --> ch
cout<<ch<<endl;
return 0;
}
相关文章推荐
- C++获取计算机主机信息(计算机名称、IP)
- PHP获取当前页面的名称,主机名,URL完整地址,URL参数,获取IP
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
- C++获取计算机主机信息(计算机名称、IP)
- 获取主机ip及MAC地址、浏览器名称
- 获取指定名称主机的IP
- C# 根据局域网IP获取主机名称
- Qt 获取本地IP 和 本地主机名称
- 获取客户机的IP和主机名称
- js获取mac地址及ip,获取计算机名称及计算机的用户名
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
- JAVA获取计算机的IP、名称、操作系统等信息
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
- IP主机名的获取
- C#编程之 如何获取本地和远程主机的IP及MAC地址(转)
- 如何用 PHP 来获取客户端 IP 和 服务器(主机)的 IP
- Python获取Windows或Linux主机名称通用函数分享
- 根据ip地址获取主机名称
- 连线被拒,请检查主机名称和埠号,并确定 postmaster 可以接受 TCP/IP 连线。