OpenProcess / win7 (war GetProcessTimes)



  • (fehler war natürlich wo anders, weil ich zwischendrin die fehler-abfragen draußen hatte und es nicht gemerkt hatte)

    Hallo,

    ich würde gerne die cpu-zeit eines Prozesses auslesen. das scheint so auch für die meisten Prozesse zu gehen...

    EDIT:
    GetProcessTimes failed, da:
    ERROR_INVALID_HANDLE6 (0x6)

    das wiederrum liegt an OpenProcess:
    ERROR_ACCESS_DENIED5 (0x5)
    bzw.
    ERROR_INVALID_PARAMETER87 (0x57)

    speziell dreht es sich um 3 Prozesse (die PID stimmt noch):
    Leerlaufprozess (System / ~1333h CPU-Zeit) [ERROR_INVALID_PARAMETER / 87]
    System (System / ~7h CPU-Zeit) [ERROR_ACCESS_DENIED / 5]
    audiodg.exe (Lokaler Dienst / ~9h CPU-Zeit) [ERROR_ACCESS_DENIED / 5]

    #pragma comment(lib, "kernel32.lib")
    #pragma comment(lib, "psapi.lib")
    
    #include <windows.h>
    #include <psapi.h>
    
    #include <string>
    
    struct task
    {
    	typedef std::size_t pid_t;
    	typedef std::string string_t;
    	typedef std::string time_t;
    
    	task(pid_t pid)
    	:	pid_(pid)
    	{
    		HANDLE hnd = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, DWORD(pid_));
    
    //GetLastError()
    		{
    			const std::size_t max_cmd_line_length = 1024;
    
    			cmd_line_.resize(max_cmd_line_length);
    			std::size_t len = max_cmd_line_length;
    			QueryFullProcessImageName(hnd, 0, &cmd_line_[0], (DWORD*)&len);
    
    			while(cmd_line_.size() > 0 && !isprint(cmd_line_.back()))
    				cmd_line_.pop_back();
    		}
    
    		{
    			FILETIME a, b, c, d;
    			GetProcessTimes(hnd, &a, &b, &c, &d);
    			//GetLastError()
    /**/
    			cpu_time_ = "0815";
    		}
    
    		CloseHandle(hnd);
    	}
    
    	pid_t pid() const
    	{
    		return pid_;
    	}
    	time_t cpu_time() const
    	{
    		return cpu_time_;
    	}
    	string_t cmd_line() const
    	{
    		return cmd_line_;
    	}
    private:
    	pid_t pid_;
    	time_t cpu_time_;
    	string_t cmd_line_;
    };
    
    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
    	const std::size_t process_count_max(2*1024);
    	const std::size_t max_cmd_line_length(1024);
    	DWORD process_id[process_count_max];
    	std::size_t process_count;
    	{
    		DWORD bytes_ret;
    		BOOL success = EnumProcesses(process_id, process_count_max, &bytes_ret);
    		if(success == FALSE)
    			throw std::runtime_error("failed to call EnumProcesses");
    		process_count = bytes_ret/sizeof(DWORD);
    		if(process_count == process_count_max)
    			throw std::runtime_error("buffer to small");
    	}
    
    	for(std::size_t i(0); i != process_count; ++i)
    	{
    		task this_process(process_id[i]);
    		std::cout << this_process.pid() << '\t' << this_process.cpu_time() << '\t' << this_process.cmd_line() << "\r\n";
    	}
    
    	system("PAUSE");
    }
    

    also hat jemand eine Ahnung, wie ich die werte für leerlaufprozess und System (und für die anderen Prozesse, an die ich nicht rankomme) bekomme?

    danke im voraus



  • Vielleicht geht es nicht, da es keine "echten" Prozesse sind, sondern vom Kernel emuliert werden (Theorie). Bei der geringen Prozessgröße würde ich auch darauf tippen, dass die User32.dll, Kernel32.dll und die NT.dll fehlen, weshalb die API möglicherweise nicht verfügbar ist?



  • Nimm WMI. Auch ist der "Idle" Prozess kein Prozess...



  • Jochen Kalmbach schrieb:

    Nimm WMI. Auch ist der "Idle" Prozess kein Prozess...

    der idle Prozess ist der einzige, bei dem mir das alles egal wäre...
    wmi... danke; obwohl ich die ganzen COM API's eigtl gar nicht mag.
    vor allem, weil man sich so unheimlich schwer dort reinfitzen kann...

    bb



  • Mit WMI bekommst Du auch keine Probleme mit x64/x86 Prozessen... da gibt es bei der "normalen" API doch bei einigen Dingen Einschränkungen...


Anmelden zum Antworten