Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A program to read a given Process's attributes ????

Status
Not open for further replies.

tevdev

Instructor
Dec 29, 2003
1
LK
Hi everyone,

This is an assignemnt I got from the institute I study. It is intended to take in the values of a process ( such as the Process ID or the Process Name ), which can be taken from the Task Manager of Windows 2000/XP, and then return the attributes of that process only, such as the CPU time, Memory Usage, Child Process time and so on.
We've been taught programming in Java for a whole semester and started on C Programming with the Turbo C++ compiler, from this sesmster onwards. I am making an attempt to do the program in C, but I'm stuck on which functions to chose. I heard that my friends are trying to do this in C++ and Visual C++ and I think that would be more feasible. But I need some help on how to get started on C++.

Which function/s in C++ do I need to use, in order to read the process table and also to return the atrributes of the Process the user asked for ?

The books that I referred to say that the process table of Win 2000 or any other for that matter, is a set of structures with one entry for each process. All my program should do is to read this set of structures,isn't it ?

P.S. >> I'm making an honest attempt to get this assignment rolling. I'd really appreciate if anyone of u guys can help me get started on this assignment. Please can you give me any links to such websites that give code for this type of projects, so that maybe I can look at it and learn from that.
Thanks.
 
Hi tevdev.

I tried to produce a program similar to taskmanager just to match process ID numbers to their respective module names, but nothing really more complicated like CPU usage or anything else. My program is written in Visual C (Visual C++ uses MFC and I tend to avoid using that). The code listing is below:
Code:
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    HANDLE hconsoleInput, hconsoleOutput;
    char string[100];
    DWORD written;
    HANDLE hSnapShot;
    PROCESSENTRY32 ProcEntry;
    BOOL hRes;

    AllocConsole();
    hconsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    hconsoleInput  = GetStdHandle(STD_INPUT_HANDLE);
    
    strcpy(string,&quot;> Running Processes...\n&quot;);
        WriteConsole(hconsoleOutput,string,strlen(string),&written,0);

    hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
    ProcEntry.dwSize = sizeof(ProcEntry);
    Process32First(hSnapShot, &ProcEntry);
    while(1)
    {
        hRes = Process32Next (hSnapShot, &ProcEntry);
      if (hRes == FALSE)
            break;
            sprintf(string,&quot;  %s %x\n&quot;, ProcEntry.szExeFile, ProcEntry.th32ProcessID);
        WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
    }
    // Pause until return pressed.
        ReadConsole(hconsoleInput,string,1,&written,0);
    return (0);
}
There are other things you could do. Try looking at some of my past threads for code on using pdh functions.

Hope that gets the ball rolling. :)

Adonai


The mind is like a parachute - it works better when open...
 
(You could try reading the structures directly, but I would not advise this. I tried doing it once and it crashed my computer. I'm not sure whether this was due to my program being all wrong or windows security violation...

Also, there is a place in the registry used for performance testing, but I can't remember the key. I suggest not using the registry directly though, as the pdh functions do the same job, and are more easier to use.)


The mind is like a parachute - it works better when open...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top