⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testharddlg.cpp

📁 自己写的测试CPU频率
💻 CPP
📖 第 1 页 / 共 2 页
字号:
LPTSTR regCPUSpeed		= "~MHz";

unsigned long cpuGetSpeed(void)
{
    TCHAR szSup[16];
    DWORD dwType;
    DWORD cbSize = sizeof(szSup);

	if( SHGetValue( HKEY_LOCAL_MACHINE,regCPUInfo,regCPUSpeed,&dwType, &szSup, &cbSize) == ERROR_SUCCESS && dwType == REG_DWORD )
	{
		return (*(DWORD*)szSup) * 1000000;
	}
	else
	{
		HANDLE	t	= GetCurrentThread();
		int		tp	= GetThreadPriority(t);
		SetThreadPriority( t,THREAD_PRIORITY_TIME_CRITICAL );
		HANDLE	p	= GetCurrentProcess();
		int		pp	= GetPriorityClass(p);
		SetPriorityClass( p,REALTIME_PRIORITY_CLASS );

		SwitchToThread();				/* get a new time spin */
		unsigned long a = rdtsc();		/* read the performance counter */
		Sleep(20);						/* pause 20 ms */
		unsigned long b = rdtsc();		/* read the performance counter */
		if(a > b)						/* we wrapped past zero */
		{
			a = b;
			Sleep(20);					/* pause 20 ms again */
			b = rdtsc();				/* read again */
		}

		unsigned long s = b - a;
		SetThreadPriority( t,tp );
		SetPriorityClass( p,pp );
		return s;
	}
}


void GetCPUInfo( COMPUTERINFO& info )
{
    DWORD dwStandard = 0;
    DWORD dwFeature = 0;
    DWORD dwMax = 0;
    DWORD dwExt = 0;
    int feature = 0, os_support = 0;
    union
    {
        char cBuf[12+1];
        struct
        {
            DWORD dw0;
            DWORD dw1;
            DWORD dw2;
        };
    } Ident;

    if (!IsCPUID())
    {
        return;
    }

    _asm
    {
        push ebx
        push ecx
        push edx

        // get the vendor string
        xor eax,eax
        cpuid
        mov dwMax,eax
        mov Ident.dw0,ebx
        mov Ident.dw1,edx
        mov Ident.dw2,ecx

        // get the Standard bits
        mov eax,1
        cpuid
        mov dwStandard,eax
        mov dwFeature,edx

        // get AMD-specials
        mov eax,80000000h
        cpuid
        cmp eax,80000000h
        jc notamd
        mov eax,80000001h
        cpuid
        mov dwExt,edx

notamd:
        pop ecx
        pop ebx
        pop edx
    }

    if (dwFeature & _MMX_FEATURE_BIT)
    {
        feature |= _CPU_FEATURE_MMX;
        if (_os_support(_CPU_FEATURE_MMX))
            os_support |= _CPU_FEATURE_MMX;
    }
    if (dwExt & _3DNOW_FEATURE_BIT)
    {
        feature |= _CPU_FEATURE_3DNOW;
        if (_os_support(_CPU_FEATURE_3DNOW))
            os_support |= _CPU_FEATURE_3DNOW;
    }
    if (dwFeature & _SSE_FEATURE_BIT)
    {
        feature |= _CPU_FEATURE_SSE;
        if (_os_support(_CPU_FEATURE_SSE))
            os_support |= _CPU_FEATURE_SSE;
    }
    if (dwFeature & _SSE2_FEATURE_BIT)
    {
        feature |= _CPU_FEATURE_SSE2;
        if (_os_support(_CPU_FEATURE_SSE2))
            os_support |= _CPU_FEATURE_SSE2;
    }
    {
		SYSTEM_INFO	system_info;
		GetSystemInfo( &system_info );
		info.cpus		= system_info.dwNumberOfProcessors;
        info.os_support = os_support;
        info.feature	= feature;
		info.frequency	= cpuGetSpeed();
        info.family		= (dwStandard >> 8)&0xF;
        info.model		= (dwStandard >> 4)&0xF;
        info.stepping	= (dwStandard >> 0)&0xF;
        Ident.cBuf[12]	= 0;
        strcpy( info.vendor_name,Ident.cBuf );
        map_mname( info.family,info.model,info.vendor_name,info.model_name );		
    }
}

void GetMEMInfo( COMPUTERINFO& info )
{
	MEMORYSTATUS	memorystatus = { 0 };
	GlobalMemoryStatus( &memorystatus );

	info.physicsmemory		= memorystatus.dwTotalPhys;
	info.freephysicsmemory	= memorystatus.dwAvailPhys;
	info.virtualmemory		= memorystatus.dwTotalVirtual;
	info.freevirtualmemory	= memorystatus.dwAvailVirtual;
}

BOOL CALLBACK monitorenumproc( HMONITOR hmonitor,HDC,LPRECT,LPARAM lparam )
{
	COMPUTERINFO* info = (COMPUTERINFO*)lparam;
	if( info->screens > 6 )
		return FALSE;

	MONITORINFOEX minfo;
	minfo.cbSize = sizeof(minfo);
	GetMonitorInfo( hmonitor,&minfo );

	//	if this screen in local at Matrox DMAX,ignore it
	int imonitor;
	if( _tcsnicmp( minfo.szDevice,_T("\\\\.\\DISPLAY"),11 ) == 0 &&
		_stscanf( minfo.szDevice+11,"%d",&imonitor ) == 1 && imonitor > 0 )
	{
		DISPLAY_DEVICE device = { sizeof(device) };
		if( EnumDisplayDevices( NULL,imonitor-1,&device,0 ) )
		{
			if( _tcsstr( (char*)device.DeviceString,_T("Matrox") ) && _tcsstr( (char*)device.DeviceString,_T("MAX") ) )
				return TRUE;	//	继续查找
		}
	}

	int w = minfo.rcMonitor.right-minfo.rcMonitor.left;
	int h = minfo.rcMonitor.bottom-minfo.rcMonitor.top;

	if( w*3 == h*4 || w*4 == h*5 )				//	单显示器
	{
		info->screen[info->screens++] = minfo.rcWork;
	}
	else if( w*3 == h*8 || w*4 == h*10 )		//	双显示器,水平排列
	{
		CRect left	= minfo.rcMonitor;	left.right	-= w/2;
		CRect right	= minfo.rcMonitor;	right.left	+= w/2;

		info->screen[info->screens++] = left	& minfo.rcWork;
		info->screen[info->screens++] = right	& minfo.rcWork;
	}
	else if( w*6 == h*4 || w*8 == h*5 )			//	垂直排列
	{
		CRect top	= minfo.rcMonitor;	top.bottom	-= h/2;
		CRect bottom= minfo.rcMonitor;	top.top		+= h/2;

		info->screen[info->screens++] = top		& minfo.rcWork;
		info->screen[info->screens++] = bottom	& minfo.rcWork;
	}
	else										//	未知的情况,按照单显示器处理
	{
		info->screen[info->screens++] = minfo.rcWork;
		TRACE(" I don't know why your display has a surprising resolvtion.\n" );
	}

	return TRUE;
}

void GetSRCInfo( COMPUTERINFO& info )
{
	info.screens = 0;
	EnumDisplayMonitors( NULL,NULL,monitorenumproc,(LPARAM)&info );	
}

BOOL CTesthardDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	char szBuf[MAX_PATH]={NULL};
	::GetLogicalDriveStrings(MAX_PATH,szBuf);
	TCHAR szTemp[3];
	CString strTemp;
	int nTemp,nCounter;	
	nTemp = nCounter = 0;
	CString str,strfree,strtotal;
	do
	{
		for(;szBuf[nCounter] != NULL; nCounter++,nTemp++)
			szTemp[nTemp] = szBuf[nCounter];
		szTemp[nTemp] = '\0';
// 		DWORD	SectorsPerCluster;
// 		DWORD	BytesPerSector;
// 		DWORD	NumberOfFreeClusters;
// 		DWORD	TotalNumberOfClusters;
// 		GetDiskFreeSpace(szTemp, &SectorsPerCluster, &BytesPerSector, &NumberOfFreeClusters, &TotalNumberOfClusters);
// 		double dbFreeMB = (NumberOfFreeClusters*SectorsPerCluster*BytesPerSector) / (1024.0F*1024.0F);
// 		double dbTotalMB = (TotalNumberOfClusters*SectorsPerCluster*BytesPerSector) / (1024.0F*1024.0F);
		
		ULARGE_INTEGER FreeBytesAvailable={0};    // bytes available to caller
		ULARGE_INTEGER TotalNumberOfBytes={0};    // bytes on disk
		ULARGE_INTEGER TotalNumberOfFreeBytes={0}; // free bytes on disk
		GetDiskFreeSpaceEx(szTemp, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
		double dbFreeMB  = (__int64)TotalNumberOfFreeBytes.QuadPart/ (1024.0F*1024.0F*1024.0F);
		double dbTotalMB = (__int64)TotalNumberOfBytes.QuadPart / (1024.0F*1024.0F*1024.0F);
		strfree.Format("%s盘空闲硬盘空间: %5.1f GB\n", szTemp,dbFreeMB );
		strtotal.Format("%s盘整个硬盘空间: %5.1f GB\n",szTemp,dbTotalMB );
		str+=strfree;
		str+=strtotal;
		nTemp = 0;
		nCounter++;
	}while(szBuf[nCounter] != NULL);

	MEMORYSTATUS MemStat;
	MemStat.dwLength = sizeof(MEMORYSTATUS);
	GlobalMemoryStatus(&MemStat);
	double freesd	= MemStat.dwAvailPhys/(1024.0F*1024.0F);
	double totalsd	= MemStat.dwTotalPhys/(1024.0F*1024.0F);

	strfree.Format("空闲物理内存: %4.1f MB\n", freesd );
	strtotal.Format("整个物理内存: %4.1f MB\n", totalsd );
	str+=strfree;
	str+=strtotal;

	COMPUTERINFO info;
	GetCPUInfo( info );
	GetMEMInfo( info );
	GetSRCInfo( info );		
	
	CString strcpu;
	strcpu.Format("CPU的数目%d\n",info.cpus);
	str+=strcpu;
	strcpu.Format("CPU的制造商%s\n"	,info.vendor_name);
	str+=strcpu;
	strcpu.Format("CPU的名字%s\n"	,info.model_name);
	str+=strcpu;
	strcpu.Format("CPU的家族号第%d代第%d款\n",info.family,info.model);
	str+=strcpu;
	strcpu.Format("CPU的修订版本第%d\n",info.stepping);
	str+=strcpu;
	strcpu.Format("CPU的特性第%d\n",info.feature);
	str+=strcpu;
	strcpu.Format("CPU的频率%1.2fG\n",info.frequency/1000000000.f);//(info.frequency/20000000.0f));
	str+=strcpu;
	strcpu.Format("操作系统支持的特性%d\n",info.os_support);
	str+=strcpu;
	strcpu.Format("总共的屏幕数%d\n",info.screens);
	str+=strcpu;
	strcpu.Format("屏幕的色深%d\n",info.colordepth);
	str+=strcpu;
	strcpu.Format("第1个屏幕可利用的位置%d,%d\n",info.screen[0].right-info.screen[0].left,info.screen[0].bottom-info.screen[0].top);
	str+=strcpu;
	::SetDlgItemText(m_hWnd,IDC_STATIC,(LPCTSTR)str);

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTesthardDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTesthardDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTesthardDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -