Solved

Get a Service Info from Task Manager

Posted on 2006-07-20
5
280 Views
Last Modified: 2013-12-03
Hi,

i used the code below to pass over all the processes and get their info.
the problem that when the process is a service then there is no info like in process.

how can i get an info on services:
- process handle
- process name
- process full name
- process Memory Info
- get process IO info

and integrate it in my code?

see my remark inside the code below.

thanks
efratk


void CProcessView::InitializeProcessMaps()
{

    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;
    char szProcessName[MAX_PATH] = "unknown";
    char szProcessFullPath[MAX_PATH] = "unknown";

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

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Get the name of the modules for each process.
      
      for ( i = 0; i < cProcesses; i++ )
      {
            HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i] );

/**************************
here process ids of services returned with hProcess == NULL thus no info about it!!!!!!!!!!!!!!!!!
************************* */            
             if ( hProcess )
            {
                  HMODULE hMod;
                  DWORD cbNeeded;

                  if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
                  {                        
                        /* GET  PROCESS INFO... /
                        //process id in:  aProcesses[i];
                        //process handle in: hProcess;
                        //Get Name
                        GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) );
                        //Get the full path to the module's file.
                        GetModuleFileNameEx( hProcess, hMod, szProcessFullPath, sizeof(szProcessFullPath));
                        //Get Process Memory Info                  
                        GetProcessMemoryInfo( hProcess, &pvwByID->psMemCounters, sizeof(pvwByID->psMemCounters));
                        //Get Process IO info
                        GetProcessIoCounters( hProcess, &pvwByID->psIOCounters);
                  }
            }
      }

      return;
}
0
Comment
Question by:efratk
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17146497
Which user account are you rinng that under? Right from the top off my head, I'd say that using

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

should fix your problem, since PROCESS_VM_READ is hardly required to gather the info you're interested in.
0
 

Author Comment

by:efratk
ID: 17162440
sorry,
i tested it without the  PROCESS_VM_READ flag and it still rerurned with hProcess = NULL;

any other idea?
0
 

Author Comment

by:efratk
ID: 17192204
i tested it also without the  PROCESS_QUERY_INFORMATION flag and it still rerurned with hProcess = NULL;

does anyone have an idea?


0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17194550
Since that is a service, one option might be to enable the debug privilege - try calling

BOOL    EnableDebugPriv (   BOOL    bEnable)
{
   HANDLE           hToken;
   TOKEN_PRIVILEGES tp;

   if   (   !OpenProcessToken   (   GetCurrentProcess   (),
                                    TOKEN_ADJUST_PRIVILEGES,
                                    &hToken
                                )
        )   return  (   FALSE);


   tp.PrivilegeCount    =   1;
   
   LookupPrivilegeValue (   NULL,
                            SE_DEBUG_NAME,
                            &tp.Privileges  [   0].Luid
                        );

   tp.Privileges    [   0].Attributes   =       bEnable
                                            ?   SE_PRIVILEGE_ENABLED
                                            :   0;

   AdjustTokenPrivileges    (   hToken,
                                FALSE,
                                &tp,
                                sizeof  (   tp),
                                NULL,
                                NULL
                            );

   return   (   GetLastError()  ==   ERROR_SUCCESS);
}

e.g. like

EnableDebugPriv ( TRUE);
0
 

Author Comment

by:efratk
ID: 17210209
thanks a lot jkr. i used the EnableDebugPriv and it helped.

great!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now