这里有几个函数,用来得到机器的信息:
1、得到注册表键值:这里判断norton的病毒库版本
void CInfoshowDlg::GetNortonVersion()
{
HKEY hkey;
LONG result = RegOpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE//INTEL//LANDesk//VirusProtect6//CurrentVersion",&hkey);
if (result != 0)
{
AfxMessageBox("无法打开注册表键值!");
return ;
}
DWORD datalen,type;
BYTE data[80];
result = RegQueryValueEx(hkey,"PatternFileDate",NULL,&type,data,&datalen);
if (result != 0)
{
AfxMessageBox("无法读取相应值!");
RegCloseKey(hkey);
return ;
}
char tmp;
int years,months,days;
tmp = data[0];
years = tmp%10;
tmp = data[1];
months = tmp + 1;
tmp = data[2];
days = tmp;
m_norton_version.Format("200%d.%d.%d",years,months,days);
RegCloseKey(hkey);
}
2、硬盘空间:
CString CInfoshowDlg::GetDisk(char *disk)
{
_ULARGE_INTEGER result_freespace,result_totalspace,result_g;
CString disk_space;
int error = 0;
int free_space,total_space;
int free_percent;
/* Get free space */
GetDiskFreeSpaceEx(disk,&result_freespace,&result_totalspace,&result_g);
if (error != 0)
{
CString t;
t.Format("GetDiskFreeSpace() error=%d",error);
AfxMessageBox(t);
}
free_space = result_freespace.QuadPart/1024/1024;
total_space = result_totalspace.QuadPart/1024/1024;
free_percent = (float)free_space/total_space*100;
disk_space.Format("%dM/ %dM (%d%%)",free_space,total_space,free_percent);
return disk_space;
}
3、取得机器名,ip地址:
void CInfoshowDlg::GetHostName()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsaData) == 0)
{
if(gethostname(name,sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name))!=NULL)
{
ip = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
}
}
WSACleanup();
}
m_ipaddr = ip;
m_hostname.Format("%s",name);
}
4、网络状态:建立一个socket,ping自己。
int CInfoshowDlg::GetNetState()
{
sockaddr_in pingSocket;
int confd,error;
m_net_state.Format("OFF");
CreateThread(NULL,0,CreatePingPort,NULL,0,0);
if(strcmp(m_ipaddr,"127.0.0.1") == 0)
{
UpdateData(FALSE);
return -1;
}
memset(&pingSocket, 0, sizeof(pingSocket));
pingSocket.sin_family = AF_INET;
pingSocket.sin_addr.s_addr = inet_addr(m_ipaddr);
pingSocket.sin_port = htons(ping_port);
confd = socket(AF_INET, SOCK_STREAM, 0);
error = connect(confd,(struct sockaddr *)&pingSocket,sizeof(sockaddr_in));
error = send(confd,"!",1,0);
if(error == 0)
{
/*
* The net is off,but we must send a char to thread that we created
*/
pingSocket.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(confd,(struct sockaddr *)&pingSocket,sizeof(sockaddr_in));
send(confd,"!",1,0);
UpdateData(FALSE);
return -1;
}
m_net_state.Format("ON");
UpdateData(FALSE);
return 0;
}