Link to home
Start Free TrialLog in
Avatar of magenta
magenta

asked on

Function call to get Processor Speed and Type?

Is there a Win32 function call to get the Processor Speed and Type? Ideally, this would be the same regardless of win9X or WinNT based systems.

Thanks,
Frank
Avatar of mite51
mite51

lurking
I am not aware of any API call for that, but www.intel.com has file called cpuinfo.zip.

This file has executable and code to find out what you want.

Disclamer: Never used it ( code portion) only know it is there...

Hope it helps...
mblat is correct in that there's no API which will return this information--you'll need to use code to do it. I would imagine that Intel CPUINFO utility wouldn't work for non-Intel processors, though, so you might want to spread your search a little wider if you want to cover the likes of AMD, VIA (nee Cyrix) and Transmeta.
Avatar of Paul Maker
gets the cpu speed, quite good too

#pragma warning(disable : 4244) /* C4244 warning expected from the
conversion between float and int in getCurrentLoad */
 
#pragma warning(disable : 4035) /* C4113 warning expected from the
theCycleCounter function */

/* creates two byes of data in the current text segment of the program
this is used to help get the CPU speed */
static inline unsigned __int64 theCycleCounter()
{      
    _asm    _emit 0x0F
    _asm    _emit 0x31 /* this implicetly pushes a return value
     on the stack, thats why i ignore the waring */
}

     /* work out the cpu speed first, this has provded to be very
     acurate (+/-  1 Mhz) */    
     unsigned __int64  start = 0;
     unsigned __int64  overhead = 0;

     start = theCycleCounter();
     Sleep(1000L);
     
     unsigned cpuspeed100 =
          (unsigned)((theCycleCounter() - start - overhead) / 10000);

     cpu_speed = cpuspeed100 / 100;    
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try using this code to get the CPU speed:

int GetProcSpeed()
{
     LONG result;
     HKEY hKey;
     DWORD data;
     DWORD dataSize;

     result = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);

     if (result == ERROR_SUCCESS)
     {
          result = RegQueryValueEx (hKey, "~MHz", NULL, NULL,
               (LPBYTE)&data, &dataSize);
     }
     else
     {
          RegCloseKey (hKey);
          return -1;
     }

     RegCloseKey (hKey);

     return (int)data;
}


Hope this helps.  Good luck!
I may have missed something (you guys have pretty complex answers, direct interaction with reg keys may depend on permissions, so I like to avoid it), but I think this is easier, and works just as well (in C, I put this with the WM_PAINT handler of WndProc):

case WM_PAINT:
 {
   //...

   SYSTEM_INFO sInfo;
   GetSystemInfo(&siSysInfo);
   TCHAR szBuffer[10];

   //begin painting...

   TextOut (hdc, 100, 100, szBuffer, wsprintf (szBuffer, TEXT ("%5d"),       siSysInfo.dwProcessorType));

   //...end painting
 } break;

You can play with it a little, I just used it to write the processor type at x=100, y=100 in the window.  Obviously szBuffer doesn't NEED to be 10 (not that I know of, as long as it's greater than the output width, ie. > 5), but I used szBuffer to do a few things in my app, too lazy to double-check the necessity...
Sorry, typo there, ammend the SYSTEM_INFO line:

   SYSTEM_INFO siSysInfo;

(insert embarrased emoticon here)