在vc中获得windows系统信息,硬盘剩余容量,内存大小
获得硬盘个数的同时获得硬盘里剩余空间
std::string GetStorgeInfo()
{
std::string storgeInfo;
int ch, drive, curdrive;
static char path[_MAX_PATH];
curdrive = _getdrive();
storgeInfo += "Available drives are: \n" ;
for( drive = 1; drive <= 26; drive++ )
if( !_chdrive( drive ) )
{
sprintf(path, "%c:\\", drive + 'A' - 1 );
storgeInfo+=path;
std::string sDisk = path;
ULARGE_INTEGER lpuse;
ULARGE_INTEGER lptotal;
ULARGE_INTEGER lpfree;
GetDiskFreeSpaceEx(sDisk.c_str() ,&lpuse,&lptotal,&lpfree);
sprintf(path, ": %4.4fGB\n",lpfree.QuadPart/1024.0/1024.0/1024.0);
storgeInfo += path;
}
return storgeInfo;
}
//获得系统cpu,内存,操作系统的信息
std::string GetSysInfo()
{
std::stringstream ss;
ss<<"内存:";
MEMORYSTATUS memStatus;
memset( &memStatus, 0x00, sizeof( MEMORYSTATUS ) );
memStatus.dwLength = sizeof( MEMORYSTATUS );
GlobalMemoryStatus( &memStatus );
SIZE_T zt = memStatus.dwTotalPhys;
ss<<(float)zt/1024.0f*1024.0f*1024.0f<<" G"<<std::endl;
ss<<"operation system:";
OSVERSIONINFO osvi;
std::string winver,os;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
switch(osvi.dwPlatformId)
{
case 0:
os = "Windows 3.x";
break;
case 1:
os = "Windows 95/98/Me";
break;
case 2:
os = "Windows XP/2000";
break;
}
char ch[255];
sprintf(ch,"Version:%d.%d Builder:%d",osvi.dwMajorVersion,osvi.dwMinorVersion,osvi.dwBuildNumber);
winver = ch;
ss<<os<<" "<<winver.c_str()<<std::endl;
ss<<"CPU:";
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
ss<<"number:"<<sysInfo.dwNumberOfProcessors<<" "<<sysInfo.dwProcessorType<<std::endl;
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
sprintf(ch,"%d * %d",x,y);
ss<<"分辨率:"<<ch<<std::endl;
std::string s = ss.str();
return s;
}
我用来测试声卡写了播放剩余的函数
sndPlaySound ("music.wav",SND_ASYNC);
PlaySound("music.wav",NULL, SND_ASYNC|SND_NODEFAULT );如果没有找到music.wav文件,第一种格式将播放系统默认的声音,第二种格式不会播放系统默认的声音
参考了下面的文章:
VC获取系统信息
在我们的软件开发中也许会需要获取相关的系统信息(如:内存信息、Windows信息、CPU信息、屏幕分辨率、磁盘信息等);下面我们就分别一个个的讲述如何获取这些信息,由于篇目的限制我们在这里就只讲述相应的方法,而对于相关API及结构体的详细信息请参见MSDN相关文档。
1. 获取内存信息
获取内存信息需要用到的API为GlobalMemoryStatus,此函数用到MEMORYSTATUS结构指针做为输出参数;从MSDN文档上我们可以知道MEMORYSTATUS结构如下:
typedef struct _MEMORYSTATUS { // mst
DWORD dwLength; // sizeof(MEMORYSTATUS)
DWORD dwMemoryLoad; // percent of memory in use
DWORD dwTotalPhys; // bytes of physical memory
DWORD dwAvailPhys; // free physical memory bytes
DWORD dwTotalPageFile; // bytes of paging file
DWORD dwAvailPageFile; // free bytes of paging file
DWORD dwTotalVirtual; // user bytes of address space
DWORD dwAvailVirtual; // free user bytes
} MEMORYSTATUS, *LPMEMORYSTATUS;
从这个结构体可以看出我们所需要的大部信息都在里在了;使用例子如下:
MEMORYSTATUS memStatus;
memset( &memStatus, 0x00, sizeof( MEMORYSTATUS ) );
memStatus.dwLength = sizeof( MEMORYSTATUS );
GlobalMemoryStatus( &memStatus );
2. 获取Windows信息
1) 获取Windows目录
所用函数为GetWindowsDirectory,使用例子如下:
char windir[MAX_PATH];
GetWindowsDirectory(windir,MAX_PATH);
2) 获取系统目录
所用函数为GetSystemDirectory,使用例子如下:
char sysdir[MAX_PATH];
GetSystemDirectory(sysdir,MAX_PATH);
3) Windows平台及Windows版本号
所用函数为GetVersionEx,此函数用到OSVERSIONINFO结构指针做为输出参数;从MSDN文档上我们可以知道OSVERSIONINFO结构如下:
typedef struct _OSVERSIONINFO{
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[ 128 ];
} OSVERSIONINFO;
从这个结构体中可以看出我们所需要的Windows平台及Windows版本号信息都在这里面了,使用例子如下:
OSVERSIONINFO osvi;
CString winver,os;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
switch(osvi.dwPlatformId)
{
case 0:
os = "Windows 3.x";
break;
case 1:
os = "Windows 95/98/Me";
break;
case 2:
os = "Windows NT/2000";
break;
}
winver.Format("Version:%d.%d Builder:%d",osvi.dwMajorVersion,osvi.dwMinorVersion,osvi.dwBuildNumber);
3. 获取CPU信息
获取CPU信息需要用GetSystemInfo此函数用到一个SYSTEM_INFO的结构指针做为输出参数;从MSDN文档上我们可以知道SYSTEM_INFO结构如下:
typedef struct _SYSTEM_INFO { // sinf
union {
DWORD dwOemId;
struct {
WORD wProcessorArchitecture;
WORD wReserved;
};
};
DWORD dwPageSize;
LPVOID lpMinimumApplicationAddress;
LPVOID lpMaximumApplicationAddress;
DWORD dwActiveProcessorMask;
DWORD dwNumberOfProcessors;
DWORD dwProcessorType;
DWORD dwAllocationGranularity;
WORD wProcessorLevel;
WORD wProcessorRevision;
} SYSTEM_INFO;
本结构体中的dwNumberOfProcessors表示CPU数量,dwProcessorType表示处理器类型,wProcessorLevel表示处理器级别,wProcessorRevision表示CPU版本;本函数的使用例子如下:
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
4. 获取屏幕分辨率
获取屏幕分辨率用到GetSystemMetrics函数,我个函数相对简单;使用例子如下:
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
CString scrxy;
scrxy.Format("%d * %d",x,y);
5. 获取磁盘信息
获取磁盘信息主要用到_getdrive、_chdrive、_getdcwd等函数;在MSDN文档上有一个例子说明了一切;其例子如下:
/* GETDRIVE.C illustrates drive functions including:
* _getdrive _chdrive _getdcwd
*/
#include <stdio.h>
#include <conio.h>
#include <direct.h>
#include <stdlib.h>
#include <ctype.h>
void main( void )
{
int ch, drive, curdrive;
static char path[_MAX_PATH];
/* Save current drive. */
curdrive = _getdrive();
printf( "Available drives are: \n" );
/* If we can switch to the drive, it exists. */
for( drive = 1; drive <= 26; drive++ )
if( !_chdrive( drive ) )
printf( "%c: ", drive + 'A' - 1 );
while( 1 )
{
printf( "\nType drive letter to check or ESC to quit: " );
ch = _getch();
if( ch == 27 )
break;
if( isalpha( ch ) )
_putch( ch );
if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
printf( "\nCurrent directory on that drive is %s\n", path );
}
/* Restore original drive.*/
_chdrive( curdrive );
printf( "\n" );
}
Output
Available drives are:
A: B: C: L: M: O: U: V:
Type drive letter to check or ESC to quit: c
Current directory on that drive is C:\CODE
Type drive letter to check or ESC to quit: m
Current directory on that drive is M:\
Type drive letter to check or ESC to quit: