c语言在windows下如何取得机器名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言在windows下如何取得机器名相关的知识,希望对你有一定的参考价值。

请高手尽量详细,谢谢
或者说怎么机器名赋给一个变量
用vc6.0编译器
多谢湛蓝水晶 ,请问如何取注册表啊.我是个新人,请指教,我只须要如何得到机器名并赋给一个变量就可以了,谢谢
似水流年00,我测试过这个代码报错太多了。

gethostname()函数就可以啊!!!

int gethostname(char *name, size_t len)这个函数,调用后,会将主机名保存在name里面。而len是name的大小。
例程,编译后只需要运行就知道自己的主机名字了。
#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;
参考技术A 会用VC读取注册表吗?
读取
HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\ComputerName\\ActiveComputerName下的ComputerName项即可得到机器名。
参考技术B 什么意思?
不明白耶!

如何在windows下搭建c语言开发环境,不使用ide。

tiny c compiler压缩包大小不超1MB,解压后设置一下PATH环境变量即可使用。
号称最快的c编译器。作者真乃神人!
http://download.savannah.gnu.org/releases/tinycc/
参考技术A 下载安装一个编译器,如MinGW,然后配置一下环境变量。

以上是关于c语言在windows下如何取得机器名的主要内容,如果未能解决你的问题,请参考以下文章

c语言中如何获得字符串的第n个字符?

windows下如何取得完整的已安装程序列表

在c语言中如何取得cpuid

windows下通过Chocolatey安装或升级node.js

windoes cmake与nmake

windos下 curl命令使用