获取计算机的名称(方法二)
2009-10-13 10:47
211 查看
第二种方法使用gethostname,需要使用WinSock。
运行结果:
这种方法获取的计算机名吃小写的。
#include <WinSock2.h> #include <stdio.h> #pragma comment(lib, "Ws2_32.lib") #define INFO_BUFFER_SIZE 100 int 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. */ return 1; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return 1; } /* The WinSock DLL is acceptable. Proceed. */ char infoBuf[INFO_BUFFER_SIZE]; //If no error occurs, gethostname returns zero. Otherwise, it returns SOCKET_ERROR if (gethostname(infoBuf, INFO_BUFFER_SIZE)==SOCKET_ERROR) { printf("SOCKET_ERROR/n"); } printf("nComputer name: %s/n", infoBuf); return 0; }
运行结果:
这种方法获取的计算机名吃小写的。
相关文章推荐
- 获取计算机的名称(方法一)
- ASP.NET简单获取服务端和客户端计算机名称的方法
- shell 获取脚步名称方法
- C# 获取 "代码被调用的一系列方法的名称"
- 织梦dedecms获取当前栏目路径及栏目名称的方法
- 获取SQL数据库中某个表中的所有字段名称的通用方法
- python获取本地计算机名字的方法
- 记忆OSI RM 计算机网络模型名称的方法
- 通过js获取上传的图片信息(临时保存路径,名称,大小)然后通过ajax传递给后端的方法
- jquery动态添加以及遍历option并获取特定样式名称的option方法
- 【方法】lwip-2.0.2配置DHCP自动从路由器获取IP地址和DNS服务器地址, 并设置NetBIOS计算机名
- VB6.0获取计算机名 用户名最简单的方法
- c#获取数据库中所有表名称的方法
- C# 获取方法内参数名称
- .net环境获取数据表名称集合的方法
- 获取iOS项目名称及版本号以及网络类型的方法
- 获取表格的方法:sheet1$不是一个有效的名称,请确保它不包括无效的字符或标点,或字符不要太长
- yii2 视图获取当前控制器的方法名称
- C# 通过属性名称获取(读取)Json对象属性值的方法