CPU speed

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • RE: CPU speed

      A registry key is easilly tweaked by anyone running REGEDIT. It's a better idea to detect it on your own...and there's nothing wrong with inline assembler.
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • FYI
      The registry key where Windows stores the CPU's frequency is:

      Source Code

      1. HKEY hKey = 0;
      2. LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
      3. _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
      4. 0, KEY_EXECUTE, &hKey);
      5. if (result == ERROR_SUCCESS)
      6. {
      7. DWORD dwType = 0;
      8. DWORD dwFreqMHz = 0;
      9. DWORD dwLength = sizeof(DWORD);
      10. result = RegQueryValueEx(hKey, _T("~MHz"), 0, &dwType,
      11. reinterpret_cast<LPBYTE>(&dwFreqMHz), &dwLength);
      12. if (result == ERROR_SUCCESS && dwType == REG_DWORD)
      13. {
      14. __int64 regFreqHz = dwFreqMHz * 1000000;
      15. // do stuff with frequency here...
      16. }
      17. RegCloseKey(hKey);
      18. }
      Display All
    • Just curious - does this work on Win9x?
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Originally posted by dpoon
      Can anyone who runs win9x machines check and post the answer here. I'm curious myself too :D
      I'm not confident enough in Win programming that I'd be sure I'd used the above code in the correct way. However if you post working test app then I'll run it on my Win98 box.
    • Ok here's a sample Win32 application that will read the CPU frequency from the value stored in the registry.

      C Source Code

      1. #include <windows.h>
      2. #include <stdio.h>
      3. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
      4. {
      5. HKEY hKey = 0;
      6. char text[256] = {0};
      7. const char *cpuRegKey = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
      8. LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, cpuRegKey, NULL, KEY_EXECUTE, &hKey);
      9. if (result == ERROR_SUCCESS)
      10. {
      11. DWORD dwType = 0;
      12. DWORD dwFreqMHz = 0;
      13. DWORD dwLength = sizeof(DWORD);
      14. result = RegQueryValueEx(hKey, "~MHz", NULL, &dwType, (LPBYTE)&dwFreqMHz, &dwLength);
      15. if (result == ERROR_SUCCESS && dwType == REG_DWORD)
      16. {
      17. sprintf(text, "CPU Frequency is %u MHz", dwFreqMHz);
      18. }
      19. else
      20. {
      21. sprintf(text, "Unable to open the DWORD value '~MHz'", cpuRegKey);
      22. }
      23. RegCloseKey(hKey);
      24. }
      25. else
      26. {
      27. sprintf(text, "Unable to open the registry key '%s'", cpuRegKey);
      28. }
      29. MessageBox(NULL, text, "CPU Frequency", MB_OK);
      30. return 0;
      31. }
      Display All
    • RE: CPU speed

      Nothing "wrong" with it, but many find assembly intimidating to say the least.

      Personally I think that a semester with the assembler should be required before any other languages can be taken. If you don't see what's happening under the hood it's hard to see what's really happening.

      For anyone who hasn't used it at least a little, I suggest Assembly Language Step-by-Step by Jeff Dunteman. It's enough to give some perspective....
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."
    • Questions for Mr. Mike and Nebuchadnezzar

      From Mr. Mike on pg. 210
      Calculating CPU Speed
      You'd think that grabbing the CPU speed form a Wintel box would be as easy as reading the system information. It seems completely crazy that this value has to be calculated.


      Were you meaning that you should be able to read a contstant value from the processor? If so, surely this wouldn't work with people overclocking their machines, having different case tempatures etc.

      Originally posted by Nebuchadnezzar
      For anyone who hasn't used it at least a little, I suggest Assembly Language Step-by-Step by Jeff Dunteman. It's enough to give some perspective....


      That would be me, this one:
      Assembly Language Step-by-step: Programming with DOS
      or this one?:
      Assembly Language: Step-By-Step (Coriolis Group Book)

      The post was edited 1 time, last by Gerry ().

    • RE: Questions for Mr. Mike and Nebuchadnezzar

      I'm not sure I've seen the first. I was only aware of the one by Coriolis, now in it's second edition (which added some info on assembly under linux). I own the first edition of this one.

      Other good ones include:

      The Revolutionary Guide to Assembly Language - Wrox
      Using Assembly Language - Que
      Advanced Assembly Language - Que
      Assembly Language for the PC - Brady
      The Undocumented PC - Addison-Wesley

      A sixth that I have in my library is Teach Yourself Assembler - MIS: Press, but this one is tougher to follow because all of its examples are too short to give a good feel for usage. Good introduction material I suppose, but I tihink Jeff's book is better in that regard. A decent quick reference since the mnemonic descriptions are concise and the examples are short.

      And, to wrap it up - the Graphics Programming Black Book by Michael Abrash - Coriolis. This one is where you take all of that new assembly knowledge and watch the master put it to proper use.

      In case you're wondering - no, I'm not an assembly guru. I have just enough knowledge to do interesting things with it, and not enough experience with it to prevent occasional re-formatting....

      My problem with assembly is that it's far too easy to do catastrophic damage if you don't watch yourself. And since it's insanely fast you'll never catch it in time if something goes wrong.
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."

      The post was edited 2 times, last by Nebuchadnezzar ().

    • RE: Questions for Mr. Mike and Nebuchadnezzar

      Originally posted by Gerry
      From Mr. Mike on pg. 210
      Calculating CPU Speed
      You'd think that grabbing the CPU speed form a Wintel box would be as easy as reading the system information. It seems completely crazy that this value has to be calculated.


      Were you meaning that you should be able to read a contstant value from the processor? If so, surely this wouldn't work with people overclocking their machines, having different case tempatures etc.


      I read that as the system should calculate the processor speed either at install or at boot and store it. There is a notation of the processor speed in the System Information page - and it seems to be accurate whether overclocked or not. This info should be accessible, but it seems that the system grabs it and stores it in a local variable instead of a reg key somewhere. Wonder if anyone up at Redmond would care to give us a few pointers....
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."