C语言怎么获取完整的计算机名称
发布网友
发布时间:2022-03-01 08:39
我来回答
共4个回答
热心网友
时间:2022-03-01 10:08
用GetComputerNameEx()函数。
函数原型:
BOOL WINAPI GetComputerNameEx(
_In_ COMPUTER_NAME_FORMAT NameType,
_Out_ LPTSTR lpBuffer,
_Inout_ LPDWORD lpnSize
);
参数 NameType 名称类型,lpBuffer 接收名字的缓冲区指针 lpnSize接收名字的缓冲区大小
返回值:成功返回非0值,失败返回0
实例:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain(void)
{
TCHAR buffer[256] = TEXT("");
TCHAR szDescription[8][32] = {TEXT("NetBIOS"),
TEXT("DNS hostname"),
TEXT("DNS domain"),
TEXT("DNS fully-qualified"),
TEXT("Physical NetBIOS"),
TEXT("Physical DNS hostname"),
TEXT("Physical DNS domain"),
TEXT("Physical DNS fully-qualified")};
int cnf = 0;
DWORD dwSize = sizeof(buffer);
for (cnf = 0; cnf < ComputerNameMax; cnf++)
{
if (!GetComputerNameEx((COMPUTER_NAME_FORMAT)cnf, buffer, &dwSize))
{
_tprintf(TEXT("GetComputerNameEx failed (%d)\n"), GetLastError());
return;
}
else _tprintf(TEXT("%s: %s\n"), szDescription[cnf], buffer);
dwSize = _countof(buffer);
ZeroMemory(buffer, dwSize);
}
}
热心网友
时间:2022-03-01 11:26
用GetComputerNameEx(). 参数ComputerNameDnsFullyQualified。 MSDN上有完整的例子,这里转贴如下:
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
int main(int argc, char** argv)
{
TCHAR buffer[256] = TEXT("");
TCHAR szDescription[8][32] = {TEXT("NetBIOS"),
TEXT("DNS hostname"),
TEXT("DNS domain"),
TEXT("DNS fully-qualified"),
TEXT("Physical NetBIOS"),
TEXT("Physical DNS hostname"),
TEXT("Physical DNS domain"),
TEXT("Physical DNS fully-qualified")};
int cnf = 0;
DWORD dwSize = sizeof(buffer);
for (cnf = 0; cnf < ComputerNameMax; cnf++)
{
if (!GetComputerNameEx((COMPUTER_NAME_FORMAT)cnf, buffer, &dwSize))
{
_tprintf(TEXT("GetComputerNameEx failed (%d)\n"), GetLastError());
return -1;
}
else _tprintf(TEXT("%s: %s\n"), szDescription[cnf], buffer);
dwSize = _countof(buffer);
ZeroMemory(buffer, dwSize);
}
return 0;
}
热心网友
时间:2022-03-01 13:01
hostname是个包含主机名/域名的字符串,函数通过给定的主机名,返回一个包含主机名称和主机地址信息的指针结构体。
、用这个 。希望采纳
热心网友
时间:2022-03-01 14:52
是不是有GetComputerDomain()函数?