#include "windows.h"
#include "stdio.h"
#include "iphlpapi.h"
#include "Nb30.h"
#pragma comment( lib, "iphlpapi.lib" )
#pragma comment( lib, "Netapi32.lib")
#define BUFSIZE 80
#define ALLOCATE_FROM_PROCESS_HEAP( bytes ) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bytes )
#define DEALLOCATE_FROM_PROCESS_HEAP( ptr ) if( ptr ) HeapFree(GetProcessHeap(), 0, ptr )
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff[30];
} ASTAT,* PASTAT;
void GetOSVersion() //获取操作系统版本
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
char OsVersion[128];
// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
{
printf("GetSystemVersion failed!/n");
return;
}
}
switch (osvi.dwPlatformId)
{
// Test for the Windows NT product family.
case VER_PLATFORM_WIN32_NT:
// Test for the specific product family.
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
strcpy(OsVersion,"Microsoft Windows Server 2003 family, ");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
strcpy (OsVersion,"Microsoft Windows XP ");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
strcpy(OsVersion,"Microsoft Windows 2000 ");
if ( osvi.dwMajorVersion <= 4 )
strcpy(OsVersion,"Microsoft Windows NT ");
// Test for specific product on Windows NT 4.0 SP6 and later.
if( bOsVersionInfoEx )
{
// Test for the workstation type.
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
if( osvi.dwMajorVersion == 4 )
strcat(OsVersion, "Workstation 4.0 " );
else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
strcat( OsVersion,"Home Edition " );
else
strcat( OsVersion,"Professional " );
}
// Test for the server type.
else if ( osvi.wProductType == VER_NT_SERVER )
{
if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2
)
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
strcat(OsVersion,"Datacenter Edition " );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
strcat(OsVersion,"Enterprise Edition " );
else if( osvi.wSuiteMask == VER_SUITE_BLADE )
strcat(OsVersion,"Web Edition " );
else
strcat(OsVersion,"Standard Edition " );
}
else if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion
== 0 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
strcat(OsVersion,"Datacenter Server " );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
strcat(OsVersion, "Advanced Server " );
else
strcat(OsVersion,"Server " );
}
else // Windows NT 4.0
{
if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
strcat(OsVersion,"Server 4.0, Enterprise Edition " );
else
strcat(OsVersion,"Server 4.0 " );
}
}
}
else // Test for specific product on Windows NT 4.0 SP5 and earlier
{
HKEY hKey;
char szProductType[BUFSIZE];
DWORD dwBufLen=BUFSIZE;
LONG lRet;
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SYSTEM//CurrentControlSet//Control//ProductOptions",
0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return;
lRet = RegQueryValueEx( hKey, "ProductType", NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
return;
RegCloseKey( hKey );
if ( lstrcmpi("WINNT", szProductType) == 0 )
strcat(OsVersion, "Workstation " );
if ( lstrcmpi("LANMANNT", szProductType) == 0 )
strcat(OsVersion,"Server " );
if ( lstrcmpi("SERVERNT", szProductType) == 0 )
strcat(OsVersion,"Advanced Server " );
printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
}
// Display service pack (if any) and build number.
if( osvi.dwMajorVersion == 4 && lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 )
{
HKEY hKey;
long lRet;
lRet=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE//Microsoft//Windows NT//CurrentVersion//Hotfix//Q246009",0,KEY_QUERY_VALUE,&hKey);
if(lRet==ERROR_SUCCESS)
{
printf( "Service Pack 6a (Build %d)/n",osvi.dwBuildNumber & 0xFFFF );
}
else{
printf( "%s (Build %d)/n",osvi.szCSDVersion,osvi.dwBuildNumber & 0xFFFF);
}
RegCloseKey(hKey);
}
else // Windows NT 3.51 and earlier or Windows 2000 and later
{
// printf( "%s (Build %d)/n",
// osvi.szCSDVersion,
// osvi.dwBuildNumber & 0xFFFF);
char temp[10];
strcat(OsVersion," ");
strcat(OsVersion,osvi.szCSDVersion);
_itoa(osvi.dwBuildNumber & 0xFFFF,temp,10);
strcat(OsVersion," (Build");
strcat(OsVersion,temp);
strcat(OsVersion,")/n");
}
break;
// Test for the Windows 95 product family.
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
strcpy(OsVersion,"Microsoft Windows 95 ");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] =='B' )
strcat(OsVersion," OSR2 " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
strcpy(OsVersion,"Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1] == 'A' )
strcat(OsVersion," SE " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
strcpy(OsVersion,"Microsoft Windows Millennium Edition/n");
}
break;
case VER_PLATFORM_WIN32s:
strcpy(OsVersion,"Microsoft Win32s/n");
break;
}
printf("OS: ");
printf(OsVersion);
}
void GetMacAddress(DWORD dwIndex) //获取MAC硬件地址
{
ASTAT Adapter;
NCB ncb;
UCHAR uRetCode;
LANA_ENUM lana_enum;
memset(&ncb,0,sizeof(ncb));
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (unsigned char *)&lana_enum;
ncb.ncb_length = sizeof(lana_enum);
uRetCode = Netbios(&ncb);
memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lana_enum.lana[dwIndex];
uRetCode = Netbios(&ncb);
memset(&ncb, 0, sizeof(ncb));
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lana_enum.lana[dwIndex];
strcpy((char *)ncb.ncb_callname, "* ");
ncb.ncb_buffer = (unsigned char *) &Adapter;
ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios(&ncb);
printf("MAC Address: %02x:%02x:%02x:%02x:%02x:%02x/n",Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5]);
}
void GetNetAdapterInfo() //获取IP地址,子网掩码,网关等
{
IP_ADAPTER_INFO * pAdptInfo = NULL;
IP_ADAPTER_INFO * pNextAd = NULL;
ULONG ulLen=0;
DWORD dwCount=0;
DWORD dwRet;
dwRet = GetAdaptersInfo( pAdptInfo, &ulLen );
if( dwRet == ERROR_BUFFER_OVERFLOW )
{
pAdptInfo = ( IP_ADAPTER_INFO* )ALLOCATE_FROM_PROCESS_HEAP( ulLen
);
dwRet = GetAdaptersInfo( pAdptInfo, &ulLen );
}
pNextAd = pAdptInfo;
while( pNextAd )
{
dwCount++;
pNextAd = pNextAd->Next;
}
printf("Network Adapter:/n");
if( dwRet == ERROR_SUCCESS )
{
pNextAd = pAdptInfo;
while( pNextAd )
{
//网卡描述
IP_ADDR_STRING * pNext = NULL;
printf("Description: %s/n",pNextAd->Description);
//获得MAC地址
GetMacAddress(-- dwCount);
//IP地址和子网掩码
pNext = &( pNextAd->IpAddressList);
printf("IP Address: %s/n",pNext->IpAddress.String);
printf("SubNet Mask: %s/n",pNext->IpMask.String);
//默认网关
pNext = &( pNextAd->GatewayList );
printf("Default Gateway: %s/n",pNext->IpAddress.String);
printf("/n");
pNextAd = pNextAd->Next;
}
}
DEALLOCATE_FROM_PROCESS_HEAP( pAdptInfo );
}
void GetDiskInfo() //得到磁盘信息
{
DWORD lInfoSize = GetLogicalDriveStrings(0, NULL);//总磁盘名大小
char * chDriver;
size_t lDriveSize;
chDriver =(char *) malloc(lInfoSize + sizeof(""));
if(GetLogicalDriveStrings(lInfoSize,chDriver) != lInfoSize - 1)
{
printf("Cannot get disk information./n");
return;
}
printf("Disk Information:/n");
lDriveSize = strlen(chDriver);//每个磁盘名大小
while( lDriveSize > 0 )//循环来得到所有分区名,如c:/,d:/...
{
UINT nType;
ULARGE_INTEGER nTotalBytes, nTotalFreeBytes, nTotalAvailable;
printf("%s ",chDriver);
nType = GetDriveType(chDriver);
switch(nType)
{
case DRIVE_FIXED:
printf("(FIXED) ");
break;
case DRIVE_CDROM:
printf("(CDROM)/n");
break;
case DRIVE_RAMDISK:
printf("(RAMDISK)/n");
break;
case DRIVE_REMOTE:
printf("(REMOTE)/n");
break;
case DRIVE_REMOVABLE:
printf("(REMOVABLE)/n");
break;
case DRIVE_UNKNOWN:
printf("(UNKNOWN)/n");
break;
}
if(nType == DRIVE_FIXED)
{
if(GetDiskFreeSpaceEx(chDriver, &nTotalAvailable,
&nTotalBytes, &nTotalFreeBytes))//得到磁盘空间信息
{
printf("Total Size: ");
printf("%ld MB,",nTotalBytes.QuadPart/(1024*1024));//总大小
printf("Free Size: ");
printf("%ld MB/n",nTotalFreeBytes.QuadPart/(1024*1024));//剩余大小
}
}
chDriver += lDriveSize + 1;
lDriveSize = strlen(chDriver);
}
}
//得到运行时间
void GetRunningTime()
{
DWORD dwTime;
int nDay,nHour,nMinute;
dwTime = GetTickCount();
nMinute = dwTime / 60000;
nHour = nMinute / 60;
nMinute = nMinute - nHour * 60;
nDay = nHour / 24;
printf("Running Time: ");
printf("%d day(s),",nDay);
printf("%d hour(s),",nHour);
printf("%d minute(s)",nMinute);
printf("/n");
}
//得到计算机名
void GetMyComputerName()
{
LPTSTR lpszName;
DWORD dwSize = 1024;
TCHAR tchBuffer[1024];
lpszName = tchBuffer;
GetComputerName(lpszName,&dwSize);
printf("Computer Name: ");
printf("%s",lpszName);
printf("/n");
}
//得到当前用户名
void GetCurrentUser()
{
LPTSTR lpszName;
DWORD dwSize = 1024;
TCHAR tchBuffer[1024];
lpszName = tchBuffer;
GetUserName(lpszName,&dwSize);
printf("Current User: ");
printf("%s",lpszName);
printf("/n");
}
//得到内存信息
void GetMemoryInfo()
{
long lVar;
MEMORYSTATUS memoryStatus;
memset(&memoryStatus, sizeof (MEMORYSTATUS), 0);
memoryStatus.dwLength = sizeof (MEMORYSTATUS);
GlobalMemoryStatus (&memoryStatus);
lVar = memoryStatus.dwTotalPhys /(1024*1024);
printf("Total Memory: ");
printf("%ld MB/n",lVar);
lVar = memoryStatus.dwAvailPhys /(1024*1024);
printf("Available Memory: ");
printf("%ld MB/n",lVar);
}
void GetCPUInfo()
{
long lRet;
HKEY hKey;
TCHAR cCPUData[64];
DWORD dwSize=sizeof(cCPUData);
lRet=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Hardware//Description//System//CentralProcessor//0",0,KEY_QUERY_VALUE,&hKey);
if(lRet==ERROR_SUCCESS)
{
RegQueryValueEx(hKey,"ProcessorNameString",NULL,NULL,(LPBYTE)cCPUData,&dwSize);
printf("CPU:");
printf("%s",cCPUData);
}
else
{
printf("Unkown/n");
}
RegCloseKey(hKey);
printf("/n");
}
main()
{
printf("/r/n");
printf("Detect System Information:");
printf("/r/n");
printf("/r/n");
GetOSVersion();
GetRunningTime();//得到运行时间
GetMyComputerName();//得到计算机名
GetCurrentUser();//得到当前用户名
GetCPUInfo();
GetDiskInfo();
GetMemoryInfo();//得到内存信息
GetNetAdapterInfo();
}