Solved

Using OpenProcess to obatin a valid process handle

Posted on 2004-10-06
3
887 Views
Last Modified: 2008-01-09
I am writing an application that is tracking (cpu usage and memory usage) of other applications I wrote.  To do this I need to obatin a valid handle to the process I want to track. I am trying to do this by using some code I got from msdn that takes in the exe name.  The code will loop through the processes and I can obtain the process name to see if it the process I am looking for, but it always returns a process handle of 0x000007cc for all processes.  GetLastError returns 5 or Invalid handel.  Why is it not returning the actual processes handle?



void ProcessTracker::setName(string name)
{
    _name = name;  //used for cpu needs to be intretated with handle

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
            return;
      }

      cProcesses = cbNeeded / sizeof(DWORD);

    for ( i = 0; i < cProcesses; i++ )
    {
          TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

            HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, aProcesses[i] );

            DWORD err = GetLastError();

            if (NULL != hProcess )
            {
                  HMODULE hMod;
                  DWORD cbNeeded;

                  if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
                         &cbNeeded) )
                  {
                        GetModuleBaseName( hProcess, hMod, szProcessName,
                                                   sizeof(szProcessName)/sizeof(TCHAR) );
                  
                        if(stricmp(szProcessName,name.c_str())==0)
                        {
                              _handle = hProcess;
                              CloseHandle(hProcess);
                              break;
                        }
                  }
            }

            CloseHandle( hProcess );
    }
}
0
Comment
Question by:nicoleheritage
  • 2
3 Comments
 

Author Comment

by:nicoleheritage
ID: 12240740
By the way.  I am using Microsoft VC++ 6.0 on Win XP
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 200 total points
ID: 12241188
Are you sure OpenProcess is failing? OpenProcess should return NULL if it failes - if it doesn't the return value of GetLastError() is meaningless as it will hold the value of the last error encountered.

The fact that OpenProcess returns the same value each time is not an error. OpenProcess returns a handle which is an internal reference to an OS object. Because you are closing the handle after each iteration it is highly likely the next time you open a process you will get the same handle value back.

To illustrate - right after you do an OpenProcess on aProcesses[i] do

HANDLE hP2 = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, aProcesses[i+1] );

and add the corresponding close at the end for hP2. If you step through this you will see the second open for the next process in the list gets a different value from the first but always the same different value as you iterate.

HTH

0
 

Author Comment

by:nicoleheritage
ID: 12242942
You are right.  I moved the closehandle from the following into the class's destructor and everything worked.  

 if(stricmp(szProcessName,name.c_str())==0)
                    {
                         _handle = hProcess;
                         CloseHandle(hProcess);

thanks.  
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 133
oracle query help 18 111
program a usb light to turn on when needed 7 63
What is wrong with the below insert statement. Getting error when executing. 5 33
This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question