获得本地计算机主机名称

来源:互联网 发布:12306用的什么数据库 编辑:程序博客网 时间:2024/06/06 03:33
//获得本地计算机NetBIOS名称
CString strName = _T("");
DWORD nSize = 1024;
::GetComputerName(strName.GetBuffer(1024), &nSize);
strName.ReleaseBuffer();
CString strText = _T("");
strText.Format(_T("本地计算机的名称:\n%s"), strName);
AfxMessageBox(strText);
//初始化WinSock
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,0), &WSAData) != 0)
{
return;
}

//获得本地计算机主机名称
CString strName = _T("");
gethostname(strName.GetBuffer(1024), 1024);
strName.ReleaseBuffer();
CString strText = _T("");
strText.Format(_T("本地计算机的名称:\n%s"), strName);
AfxMessageBox(strText);


//清理WinSock
WSACleanup();


DWORD nResult = 0;


//获得需要的缓冲区大小
DWORD nLength = 0;
nResult = GetNetworkParams(NULL, &nLength);
if (nResult != ERROR_BUFFER_OVERFLOW)
{
return;
}


FIXED_INFO* pFixedInfo = (FIXED_INFO*)new BYTE[nLength];


//获得本地计算机网络参数
nResult = GetNetworkParams(pFixedInfo, &nLength);
if (nResult != ERROR_SUCCESS)
{
delete[] pFixedInfo;
return;
}


//获得本地计算机主机名称
CString strText = _T("");
strText.Format(_T("本地计算机的名称:\n%s"), pFixedInfo->HostName);
AfxMessageBox(strText);


delete[] pFixedInfo;

原创粉丝点击