API pentru stabilire viteza procesor

Acest forum este dedicat intrebarilor de Windows API, folosing C/C++
Post Reply
rdenis
Membru
Membru
Posts: 50
Joined: 10 Apr 2010, 20:16
Judet: Iaşi

API pentru stabilire viteza procesor

Post by rdenis » 27 Sep 2010, 07:55

buna tuturor,

stiti vreun API pentru a stabili viteza procesorului, sa scad si sa aflu viteza procesorului ma intereseaza in special. pe net am gasit ceva insa se bazeaza pe WMI.

multumesc anticipat,
denis



User avatar
cristianamarie
Membru++
Membru++
Posts: 480
Joined: 12 Mar 2009, 18:47
Judet: Iaşi
Location: Iasi

Re: API pentru stabilire viteza procesor

Post by cristianamarie » 27 Sep 2010, 10:21

WMI e mai safe, chiar daca e mai mult de scris. Se poate si cu PDH, dar de la Vista incolo trebuie Admin rights.
Obs. PdhAddEnglishCounterW are nevoie de Vista. Pe XP trebuie cu PDH raw, era un sample in Platform SDK sau MSDN din aprilie 1999 parca.

Code: Select all

#include <windows.h>
#include <pdh.h>
#include <stdio.h>

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

int 
wmain(int argc, wchar_t** argv) {
    DWORD dwProcessor = 0;
    BOOL fDone = FALSE;
    WCHAR szCounter[512] = L"";

    fDone = FALSE;
    for(dwProcessor = 0; !fDone; dwProcessor++) {
        PDH_HQUERY hq = NULL;
        PDH_STATUS stat;
        double dblFrequency = 0.00;

        fDone = TRUE;

        __try {
            stat = PdhOpenQueryW(NULL, 0, &hq);
            if(stat == ERROR_SUCCESS) {
                PDH_HCOUNTER hc = NULL;

                wsprintf(szCounter, L"\\Processor Performance(PPM_Processor_%d)\\Processor Frequency", dwProcessor);
                stat = PdhAddEnglishCounterW(hq, szCounter, 0, &hc);
                if(stat == ERROR_SUCCESS) {
                    stat = PdhCollectQueryData(hq);
                    if(stat == ERROR_SUCCESS) {
                        PDH_FMT_COUNTERVALUE value = {0};
                        stat = PdhGetFormattedCounterValue(hc, PDH_FMT_DOUBLE, NULL, &value);
                        if(stat == ERROR_SUCCESS) {
                            dblFrequency = value.doubleValue;
                        }
                    }
                }
            }
            if(stat != ERROR_SUCCESS) {
                __leave;
            }

            wprintf(L"Processor %2d frequency: %.2f\n", dwProcessor, dblFrequency);
            fDone = FALSE;
        }
        __finally {
            if(hq != NULL) {
                PdhCloseQuery(hq);
            }
        }
    }

    return 0;
}
Nuclear launch detected

User avatar
cristianamarie
Membru++
Membru++
Posts: 480
Joined: 12 Mar 2009, 18:47
Judet: Iaşi
Location: Iasi

Re: API pentru stabilire viteza procesor

Post by cristianamarie » 29 Sep 2010, 10:34

A mers?
Nuclear launch detected

rdenis
Membru
Membru
Posts: 50
Joined: 10 Apr 2010, 20:16
Judet: Iaşi

Re: API pentru stabilire viteza procesor

Post by rdenis » 02 Oct 2010, 10:35

buna cristianamarie...scuze, insa am fost plecat in ultimele zile si nu am avut nici access la net, cel putin nu de pe PC-ul meu...o sa testez astazi insa sunt sigur ca va merge :whorship: . mai ramane sa-l studiez :biggrin:

ideea ta e buna si interesanta in acelasi timp si imi rezolva jumatate din problema, mai trebuie doar sa vad cum sa stabilesc viteza procesorului ( ex. sa ma folosesc de Intel SpeedStep) and I'm in business :D

multumesc si o sa revin cat mai repede cu updates.

ps: exista vreun eveniment care este trimis atunci cand CPU-ul isi schimba viteza (ex. again, daca intra SpeedStep in actiune)?

doresc sa fac un programel care sa scada viteza procesorului atunci cand de ex ma uit la un film pt ca laptop-ul meu e cam vechi si ma lasa repede bateria iar pana se "gandeste" Windows sa imi scada viteza trece filmul. am observat la laptopurile Acer ca vin cu cateva programele interesante (imi scapa numele acum) si unul dintre ele face ce ma intereseaza pe mine.

User avatar
cristianamarie
Membru++
Membru++
Posts: 480
Joined: 12 Mar 2009, 18:47
Judet: Iaşi
Location: Iasi

Re: API pentru stabilire viteza procesor

Post by cristianamarie » 02 Oct 2010, 15:45

WM_POWERBROADCAST e pentru power events, poate trimite sistemul asta. Pentru processor speed change callback Win32 nu stiu sa fie, poate doar tot un WMI callback. Polling ar mai ramine.
Nuclear launch detected

User avatar
tphase
Junior
Junior
Posts: 18
Joined: 21 Oct 2012, 10:47
Judet: Timiş
Location: Timisoara
Contact:

Re: API pentru stabilire viteza procesor

Post by tphase » 17 Jun 2013, 22:23

Poftim:

Code: Select all

#include "Shellapi.h"
#include <cmath>
#include <cctype>
#include <powrprof.h>
#include <ppl.h>
#include <thread>
#include <windows.h>

Code: Select all

CString strFrequency= L"";
strFrequency.Format(_T(" %.0f"), GetCPUFrequency());
strFrequency+= " Mhz";

Code: Select all

float CMainFrame::GetCPUFrequency()
{
	try
	{
		typedef struct _PROCESSOR_POWER_INFORMATION 
		{
			ULONG  Number;
			ULONG  MaxMhz;
			ULONG  CurrentMhz;
			ULONG  MhzLimit;
			ULONG  MaxIdleState;
			ULONG  CurrentIdleState;
		} 
		PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;

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

		// get the number or processors 
		SYSTEM_INFO si = {0};
		::GetSystemInfo(&si);

		// allocate buffer to get info for each processor
		const int size = si.dwNumberOfProcessors * sizeof(PROCESSOR_POWER_INFORMATION);
		LPBYTE pBuffer = new BYTE[size]; 

		NTSTATUS status = ::CallNtPowerInformation(ProcessorInformation, NULL, 0, pBuffer, size);
		try
		{
			// list each processor frequency 
			PPROCESSOR_POWER_INFORMATION ppi = (PPROCESSOR_POWER_INFORMATION)pBuffer;
			for(DWORD nIndex = 0; nIndex < si.dwNumberOfProcessors; nIndex++)
			{
				return ppi->CurrentMhz;

				ppi++;
			}
		}
		catch(...)
		{
			return 0;
		}

		delete []pBuffer;
	}
	catch(...) 
	{ 
		return 0;
	}
}

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: API pentru stabilire viteza procesor

Post by Ovidiu Cucu » 01 Jul 2013, 22:18

tphase wrote:Poftim:

Code: Select all

#include "Shellapi.h"
#include <cmath>
#include <cctype>
#include <powrprof.h>
#include <ppl.h>
#include <thread>
#include <windows.h>

Code: Select all

CString strFrequency= L"";
strFrequency.Format(_T(" %.0f"), GetCPUFrequency());
strFrequency+= " Mhz";

Code: Select all

float CMainFrame::GetCPUFrequency()
{
	try
	{
		typedef struct _PROCESSOR_POWER_INFORMATION 
		{
			ULONG  Number;
			ULONG  MaxMhz;
			ULONG  CurrentMhz;
			ULONG  MhzLimit;
			ULONG  MaxIdleState;
			ULONG  CurrentIdleState;
		} 
		PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;

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

		// get the number or processors 
		SYSTEM_INFO si = {0};
		::GetSystemInfo(&si);

		// allocate buffer to get info for each processor
		const int size = si.dwNumberOfProcessors * sizeof(PROCESSOR_POWER_INFORMATION);
		LPBYTE pBuffer = new BYTE[size]; 

		NTSTATUS status = ::CallNtPowerInformation(ProcessorInformation, NULL, 0, pBuffer, size);
		try
		{
			// list each processor frequency 
			PPROCESSOR_POWER_INFORMATION ppi = (PPROCESSOR_POWER_INFORMATION)pBuffer;
			for(DWORD nIndex = 0; nIndex < si.dwNumberOfProcessors; nIndex++)
			{
				return ppi->CurrentMhz;

				ppi++;
			}
		}
		catch(...)
		{
			return 0;
		}

		delete []pBuffer;
	}
	catch(...) 
	{ 
		return 0;
	}
}
@tphase
Scuze, poate nu vad eu bine!
Ce exceptii trebuie sa ne asteptam sa prindem in cele doua catch(...)-uri din codul tau?

Post Reply