Git   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了获取计算机名称和login的用户名大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在开发一个应用程序。 其中一种方法需要捕获login到计算机上的计算机名称和@L_262_3@,然后同时显示给@L_262_3@。 我需要它在Windows和Linux上运行。 做这个的最好方式是什么?

SetThreadAffinitymask被忽略…任何想法?

无法复制Java中的“我的文档”

静态variables在任何特定的线程初始化?

为什么对BeginPaint()的调用总是生成一个WM_NCPAINT消息?

项目使用两个不@R_33_11197@的sql Server

视窗

你可以尝试使用GetComputerName和GetUserName ,这里是一个例子:

#define INFO_BUFFER_SIZE 32767 TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; // Get and display the name of the computer. if( !GetComputerName( infoBuf,&bufCharCount ) ) printError( TEXT("GetComputerName") ); _tprintf( TEXT("nComputer name: %s"),infoBuf ); // Get and display the user name. if( !GetUserName( infoBuf,&bufCharCount ) ) printError( TEXT("GetUserName") ); _tprintf( TEXT("nUser name: %s"),infoBuf );

请参阅: GetComputerName和GetUserName

Linux的

使用gethostname获取计算机名称(请参阅gethostname ),使用getlogin_r获取登录用户名。 您可以在getlogin_r的手册页查看更多信息。 简单的@L_675_18@如下:

#include <unistd.h> #include <limits.h> char hostname[host_name_MAX]; char username[LOGIN_NAME_MAX]; gethostname(hostname,host_name_MAX); getlogin_r(username,LOGIN_NAME_MAX);

在POSIX系统上,您可以使用在unistd.h声明的gethostname和getlogin函数

/* This is a C program (I've seen the C++ tag too latE). ConverTing it to a pretty C++ program is left as an exercise to the reader. */ #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { char hostname[host_name_MAX]; char username[LOGIN_NAME_MAX]; int result; result = gethostname(hostname,host_name_MAX); if (result) { perror("gethostname"); return EXIT_FAILURE; } result = getlogin_r(username,LOGIN_NAME_MAX); if (result) { perror("getlogin_r"); return EXIT_FAILURE; } result = printf("Hello %s,you are logged in to %s.n",username,hostName); if (result < 0) { perror("printf"); return EXIT_FAILURE; } return EXIT_succesS; }

可能的输出

Hello 5gon12eder,you are logged in to example.com.

这似乎比依赖并不总是存在的环境变量更安全。

我正在撤回最后的声明,因为

getlogin的手册页实际上不鼓励它的使用,而赞成getenv("LOGIN")和

上面的程序中的getlogin_r调用失败, ENOTTY从Emacs中运行程序而不是交互式终端,而getenv("USER")在两种情况下都能正常工作。

Windows环境中,您可以使用getenv("COMPUTERNAME") , getenv("USERNAME")


Linux中 – getenv("HOSTNAME") , getenv("USER")

请参阅getenv参

使用gethostname()来获取计算机名称,同时支持Windows和Linux 。

关于Denis的回答,请注意,用于Linux的getenv("HOSTNAME") 可能不总是工作,因为环境变量可能不会被导出到程序 。

多平台C ++代码示例来获取计算机名称(这是我的Win7和CentOS机器的工作原理):

char *temp = 0; std::string computerName; #if Defined(WIN32) || defined(_WIN32) || defined(_WIN64) temp = getenv("COMPUTERNAME"); if (temp != 0) { computerName = temp; temp = 0; } #else temp = getenv("HOSTNAME"); if (temp != 0) { computerName = temp; temp = 0; } else { temp = new char[512]; if (gethostbyname(temp,512) == 0) { // success = 0,failure = -1 computerName = temp; } delete []temp; temp = 0; } #endif

如果你可以使用Boost,你可以这样做来轻松获取主机名:

#include <boost/asio/ip/host_name.hpp> // ... whatever ... auto host_name = boost::asio::ip::host_name();

大佬总结

以上是大佬教程为你收集整理的获取计算机名称和login的用户名全部内容,希望文章能够帮你解决获取计算机名称和login的用户名所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: