VC中如何获取本机计算机名

2024-12-15 16:25:54
推荐回答(4个)
回答1:

获得计算机名,用户名,系统路径,环境路径等。
#include
#include

#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);
}

回答2:

#include
#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;
}

}

回答3:

VC中如何获取本机计算机名
刊下外写悉临叹声碰闷

婚姻是爱情的坟墓,不过别难过,每个人最终都会走进坟墓,放心去吧,阿门!

回答4:

三种方法 长短边分别为:长6短1,长5短2,长4短3