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

📄 checkcpu.c

📁 这是一本学习 window编程的很好的参考教材
💻 C
字号:
// Base on CodePlay CheckCPU
// Added an isAthlonXP ( isPIII && isAthlon )

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include "CheckCpu.h"
#define FALSE 0
#define TRUE 1

#pragma comment(lib, "WinMM.lib")

int __cdecl AvailCPUID (void)
    {
    /* Tests for availability of CPUID by flipping ID bit eflags */
    int f=FALSE;
    __asm
	{
	pushfd
	pop eax
	mov ebx,eax
	xor eax,0x00200000
	push eax
	popfd
	pushfd
	pop eax
	push ebx
	popfd
	xor eax,ebx
	mov f,eax
	}

    return f;
    }

int __cdecl FirstCPUID (char *s)
    {
    int Level;
    // Find processor manufacturer and number of supported CPUID modes
    __asm
	{
	xor eax,eax
	cpuid
	mov Level,eax
	mov esi,s
	mov dword ptr [esi],ebx
	mov dword ptr 4[esi],edx
	mov dword ptr 8[esi],ecx
	}
    s [12] = 0;
    return Level;
    }

int __cdecl FirstAMDCPUID (void)
    {
    int Level;
    // Finds number of supported AMD extended CPUID modes
    __asm
	{
	mov eax,80000000H
	cpuid
	mov Level,eax
	}
    return Level;
    }

unsigned int __cdecl VersionCPUID (void)
    {
    unsigned int Features;
    // Finds features of CPU
    __asm
	{
	mov eax,1
	cpuid
	mov Features,edx
	}
    return Features;
    }

int __cdecl FamilyCPUID (void)
    {
    unsigned int Family;
    // Finds family of CPU
    __asm
	{
	mov eax,1
	cpuid
	mov Family,eax
	}
    return Family;
    }

unsigned int __cdecl AMDVersionCPUID (void)
    {
    unsigned int Features;
    // Finds AMD extended features of CPU
    __asm
	{
	mov eax,80000001H
	cpuid
	mov Features,edx
	}
    return Features;
    }

int __cdecl isSSEOS (void)
    {
//    int Flag;
    // Checks operating system supports SSE
    return TRUE;
/* Fails on Windows 2000 because of privelage levels
    __asm
	{
	mov eax,1
	cpuid            // This is needed or the next instruction doesn't work
			// No idea why
	mov eax,cr4
	shr eax,9
	and eax,1
	mov Flag,eax
	}
    return Flag;
*/
    }

int __cdecl isPentium (void)
    {
//    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is a Pentium or compatible.
    if (!AvailCPUID ()) return FALSE;       // no CPUID
    if (FamilyCPUID () < 5) return FALSE;   // must be 486
    Features = VersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    return TRUE;
    }

int __cdecl isPentiumMMX (void)
    {
//    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is a Pentium with MMX or compatible.
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 5) return FALSE;
    Features = VersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x800000)) return FALSE; // no MMX
    return TRUE;
    }

int __cdecl isPentiumPro (void)
    {
//    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is a Pentium Pro or compatible.
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 6) return FALSE;
    Features = VersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x8000)) return FALSE; // no CMOV/FCMOV/FCOMI
    return TRUE;
    }

int __cdecl isPentiumII (void)
    {
//    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is a Pentium II or compatible.
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 6) return FALSE;
    Features = VersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x8000)) return FALSE; // no CMOV/FCMOV/FCOMI
    if (!(Features & 0x800000)) return FALSE; // no MMX
    return TRUE;
    }

int __cdecl isPentiumIII (void)
    {
//    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is a Pentium III or compatible.
    // Returns FALSE if Pentium III with no operating system support
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 6) return FALSE;
    Features = VersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x8000)) return FALSE; // no CMOV/FCMOV/FCOMI
    if (!(Features & 0x800000)) return FALSE; // no MMX
    if (!(Features & 0x2000000)) return FALSE; // no Streaming SIMD
    if (!(isSSEOS ())) return FALSE; // no Streaming SIMD operating system support
    return TRUE;
    }

int __cdecl isPentium4 (void)
    {
//    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is a Pentium 4 or compatible.
    // Returns FALSE if Pentium 4 with no operating system support
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 6) return FALSE;
    Features = VersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x8000)) return FALSE; // no CMOV/FCMOV/FCOMI
    if (!(Features & 0x800000)) return FALSE; // no MMX
    if (!(Features & 0x2000000)) return FALSE; // no Streaming SIMD
    if (!(Features & 0x4000000)) return FALSE; // no Streaming SIMD 2
    if (!(isSSEOS ())) return FALSE; // no Streaming SIMD operating system support
    return TRUE;
    }

int __cdecl isAMDK6 (void)
    {
    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is an AMD K6 with MMX or compatible.
    // Returns FALSE if processor is a Pentium MMX, even though this
    // is a compatible processor. Use isPentiumMMX for that.
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 5) return FALSE;    // not Pentium class or above
    if (FirstCPUID (Brand) < 1) return FALSE;
    if (FirstAMDCPUID () < 0x80000001) return FALSE;
    Features = AMDVersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x800000)) return FALSE; // no MMX
    return TRUE;
    }

int __cdecl isAMDK62 (void)
    {
    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is an AMD K6 with MMX and 3DNow! or compatible.
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 5) return FALSE;    // not Pentium class or above
    if (FirstCPUID (Brand) < 1) return FALSE;
    if (FirstAMDCPUID () < 0x80000001) return FALSE;
    Features = AMDVersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x800000)) return FALSE; // no MMX
    if (!(Features & 0x80000000)) return FALSE; // no 3DNow!
    return TRUE;
    }

int __cdecl isAthlon (void)
    {
    char Brand [64];
    unsigned int Features;
    // Returns TRUE if processor is an AMD Athlon with MMX and Advanced 3DNow! or compatible.
    if (!AvailCPUID ()) return FALSE;
    if (FamilyCPUID () < 6) return FALSE;    // not Pentium Pro class or above
    if (FirstCPUID (Brand) < 1) return FALSE;
    if (FirstAMDCPUID () < 0x80000001) return FALSE;
    Features = AMDVersionCPUID ();
    if (!(Features & 0x1)) return FALSE;    // no float
    if (!(Features & 0x8000)) return FALSE; // no CMOV/FCMOV/FCOMI
    if (!(Features & 0x800000)) return FALSE; // no MMX
    if (!(Features & 0x400000)) return FALSE; // no K7 MMX
    if (!(Features & 0x80000000)) return FALSE; // no 3DNow!
    if (!(Features & 0x40000000)) return FALSE; // no Advanced 3DNow!
    return TRUE;
    }

int __cdecl isAthlonXP (void)
	{
	return isAthlon()&&isPentiumIII();
	}

void __cdecl GetCpuName(char *name)
{
    char *name1 = name;
    if (FirstAMDCPUID () < 0x80000002)
	{
	if (FirstCPUID (name) < 1)
	    strcpy (name, "Unknown");
	if (strcmp (name, "GenuineIntel") == 0)
	    {
	    if (isPentium4 ())
		strcpy (name, "Intel Pentium 4");
	    else if (isPentiumIII ())
		strcpy (name, "Intel Pentium III");
	    else if (isPentiumII ())
		strcpy (name, "Intel Pentium II");
	    else if (isPentiumPro ())
		strcpy (name, "Intel Pentium Pro");
	    else if (isPentiumMMX ())
		strcpy (name, "Intel Pentium");
	    else if (isPentium ())
		strcpy (name, "Intel Pentium");
	    }
	}
    else
	{
	__asm
	{
	    push ebx
	    mov esi,name1

	    mov  eax,0x80000002          // FIRST 16 CHRS
	    xor   ebx,ebx
	    xor   ecx,ecx
	    xor   edx,edx

	    cpuid

	    mov   [esi],eax
	    mov   [esi+4],ebx
	    mov   [esi+8],ecx
	    mov   [esi+12],edx

	    add   esi,16
	    mov   eax,0x80000003          // NEXT 16 CHRS
	    xor   ebx,ebx
	    xor   ecx,ecx
	    xor   edx,edx

	    cpuid

	    mov   [esi],eax
	    mov   [esi+4],ebx
	    mov   [esi+8],ecx
	    mov   [esi+12],edx


	    add   esi,16
	    mov   eax,0x80000004          // LAST 16 CHRS
	    xor   ebx,ebx
	    xor   ecx,ecx
	    xor   edx,edx

	    cpuid

	    mov   [esi],eax
	    mov   [esi+4],ebx
	    mov   [esi+8],ecx
	    mov   [esi+12],edx

	    mov   eax,0
	    mov   [esi+48],al
	    pop ebx
	}
    }
}

unsigned int __cdecl GetCpuSpeed(void)
{
    int                             timeStart       = 0;
    int                             timeStop        = 0;
    unsigned long   StartTicks      = 0;
    unsigned long   EndTicks        = 0;
    unsigned long   TotalTicks      = 0;
    unsigned long   cpuSpeed        = 0;

    timeStart = timeGetTime();                              //GET TICK EDGE
    for(;;)
    {
	timeStop = timeGetTime();
	if ( (timeStop-timeStart) > 1 )         // ROLLOVER PAST 1
	{
	    __asm{
		 push ebx
		 xor    eax, eax
		 xor    ebx, ebx
		 xor    ecx, ecx
		 xor    edx, edx
		 cpuid
		 rdtsc
		 mov    [StartTicks], eax
		 pop ebx
		}
	    break;
	}
    }

    timeStart = timeStop;

    for(;;)
    {
	timeStop = timeGetTime();
	if ( (timeStop-timeStart) > 1000 )      //ONE SECOND
	{
	    __asm{
		 push ebx
		 xor    eax, eax
		 xor    ebx, ebx
		 xor    ecx, ecx
		 xor    edx, edx
		 cpuid
		 rdtsc
		 mov    [EndTicks], eax
		 pop ebx
		}

	    break;
	}
    }

    TotalTicks = EndTicks-StartTicks;               // TOTAL

    cpuSpeed = TotalTicks;                  // SPEED


    return (UINT)cpuSpeed;
}

⌨️ 快捷键说明

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