怎么获取和设置主机信息?
我想通过VC中MFC封装的函数获取主机信息(主机名,用户名,子网掩码,IP,以及共享的资源),请问都需要哪些函数啊?如果设置这些信息又需要哪些函数呢?希望高手指点一二!先谢谢了!
[解决办法]
//获得本地计算机名称
int CClientDlg::GetLocalHostName(CString &sHostName)
{
char szHostName[256];
int nRetCode;
nRetCode=gethostname(szHostName,sizeof(szHostName));
if(nRetCode!=0)
{
//产生错误
sHostName=_T("没有取得");
return GetLastError();
}
sHostName=szHostName;
return 0;
}
//获得本地IP
int CClientDlg::GetIpAddress(const CString &sHostName, CString &sIpAddress)
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
//产生错误
sIpAddress=_T("");
return GetLastError();
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
sIpAddress=inet_ntoa(inAddr);//把一个inAddr结构转化为点分十进制格式
if(sIpAddress.IsEmpty())
sIpAddress=_T("没有取得");
}
return 0;
}