VC中如何获取本机计算机名
展开全部
获得计算机名,用户名,系统路径,环境路径等。
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 1024
void main()
{
LPTSTR lpszSystemInfo; // pointer to system information string
DWORD cchBuff = BUFSIZE; // size of computer or user name
TCHAR tchBuffer[BUFSIZE]; // buffer for string
DWORD dwResult; // function return value
lpszSystemInfo = tchBuffer;
// Get and display the name of the computer.
if( GetComputerName(lpszSystemInfo, &cchBuff) )
printf("Computer name: %s\n", lpszSystemInfo);
// Get and display the user name.
cchBuff = BUFSIZE;
if( GetUserName(lpszSystemInfo, &cchBuff) )
printf("User name: %s\n\n", lpszSystemInfo);
// Get and display the system directory.
if( GetSystemDirectory(lpszSystemInfo, MAX_PATH+1) )
printf("System directory: %s\n", lpszSystemInfo);
// Get and display the Windows directory.
if( GetWindowsDirectory(lpszSystemInfo, MAX_PATH+1) )
printf("Windows directory: %s\n\n", lpszSystemInfo);
// Display the "environment variables" header.
printf("Environment variables (partial list): \n");
// Expand the OS environment variable.
dwResult = ExpandEnvironmentStrings(
"OS=%OS%",
lpszSystemInfo,
BUFSIZE);
if( dwResult <= BUFSIZE )
printf(" %s\n", lpszSystemInfo);
// Expand the PATH environment variable.
dwResult = ExpandEnvironmentStrings(
"PATH=%PATH%",
lpszSystemInfo,
BUFSIZE);
if( dwResult <= BUFSIZE )
printf(" %s\n", lpszSystemInfo);
// Expand the TMP environment variable.
dwResult = ExpandEnvironmentStrings(
"TEMP=%TEMP%",
lpszSystemInfo,
BUFSIZE);
if( dwResult <= BUFSIZE )
printf(" %s\n", lpszSystemInfo);
}
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 1024
void main()
{
LPTSTR lpszSystemInfo; // pointer to system information string
DWORD cchBuff = BUFSIZE; // size of computer or user name
TCHAR tchBuffer[BUFSIZE]; // buffer for string
DWORD dwResult; // function return value
lpszSystemInfo = tchBuffer;
// Get and display the name of the computer.
if( GetComputerName(lpszSystemInfo, &cchBuff) )
printf("Computer name: %s\n", lpszSystemInfo);
// Get and display the user name.
cchBuff = BUFSIZE;
if( GetUserName(lpszSystemInfo, &cchBuff) )
printf("User name: %s\n\n", lpszSystemInfo);
// Get and display the system directory.
if( GetSystemDirectory(lpszSystemInfo, MAX_PATH+1) )
printf("System directory: %s\n", lpszSystemInfo);
// Get and display the Windows directory.
if( GetWindowsDirectory(lpszSystemInfo, MAX_PATH+1) )
printf("Windows directory: %s\n\n", lpszSystemInfo);
// Display the "environment variables" header.
printf("Environment variables (partial list): \n");
// Expand the OS environment variable.
dwResult = ExpandEnvironmentStrings(
"OS=%OS%",
lpszSystemInfo,
BUFSIZE);
if( dwResult <= BUFSIZE )
printf(" %s\n", lpszSystemInfo);
// Expand the PATH environment variable.
dwResult = ExpandEnvironmentStrings(
"PATH=%PATH%",
lpszSystemInfo,
BUFSIZE);
if( dwResult <= BUFSIZE )
printf(" %s\n", lpszSystemInfo);
// Expand the TMP environment variable.
dwResult = ExpandEnvironmentStrings(
"TEMP=%TEMP%",
lpszSystemInfo,
BUFSIZE);
if( dwResult <= BUFSIZE )
printf(" %s\n", lpszSystemInfo);
}
参考资料: http://apps.hi.baidu.com/share/detail/15894048
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
快又稳
2024-10-23 广告
2024-10-23 广告
广州快又稳网络科技有限公司是一家集技术研发、产品创新、服务优化于一体的高新技术企业。公司自成立以来,始终秉承“网络使人类缩进距离,我们让网络快又稳”的核心理念,致力于为全球客户提供高效、安全、易用的互联网解决方案及信息技术服务。在数字时代,...
点击进入详情页
本回答由快又稳提供
展开全部
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA _wsaData = {0};
char _HostName[1024] = {0};
struct hostent* _HostList = NULL;
struct in_addr _addr = {0};
int _Result = 0;
int i = 0;
_Result = WSAStartup(MAKEWORD(2,2),&_wsaData);
if(_Result == SOCKET_ERROR)
{
printf("WSAStartup failed\r\n");
return;
}
_Result = gethostname(_HostName,sizeof(_HostName));
if(_Result == SOCKET_ERROR)
{
printf("gethostname failed\r\n");
return;
}
_HostList = gethostbyname(_HostName);
printf("Hostname:%s\r\n",_HostName);
for(i=0;_HostList->h_addr_list[i] != NULL;i++)
{
memcpy(&(_addr.S_un.S_addr),(void *)_HostList->h_addr_list[i],4); // ====++++++
printf("IP:%s\r\n",inet_ntoa(_addr));
}
//记住接下来的一步很容易忽略
if(WSACleanup() == SOCKET_ERROR)
{
printf("WSACleanup failed\r\n");
return;
}
}
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA _wsaData = {0};
char _HostName[1024] = {0};
struct hostent* _HostList = NULL;
struct in_addr _addr = {0};
int _Result = 0;
int i = 0;
_Result = WSAStartup(MAKEWORD(2,2),&_wsaData);
if(_Result == SOCKET_ERROR)
{
printf("WSAStartup failed\r\n");
return;
}
_Result = gethostname(_HostName,sizeof(_HostName));
if(_Result == SOCKET_ERROR)
{
printf("gethostname failed\r\n");
return;
}
_HostList = gethostbyname(_HostName);
printf("Hostname:%s\r\n",_HostName);
for(i=0;_HostList->h_addr_list[i] != NULL;i++)
{
memcpy(&(_addr.S_un.S_addr),(void *)_HostList->h_addr_list[i],4); // ====++++++
printf("IP:%s\r\n",inet_ntoa(_addr));
}
//记住接下来的一步很容易忽略
if(WSACleanup() == SOCKET_ERROR)
{
printf("WSACleanup failed\r\n");
return;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-09-28
展开全部
VC中如何获取本机计算机名
刊下外写悉临叹声碰闷
婚姻是爱情的坟墓,不过别难过,每个人最终都会走进坟墓,放心去吧,阿门!
刊下外写悉临叹声碰闷
婚姻是爱情的坟墓,不过别难过,每个人最终都会走进坟墓,放心去吧,阿门!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-01-13
展开全部
三种方法 长短边分别为:长6短1,长5短2,长4短3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询