See Windows Process

Source: Internet
Author: User
Tags 04x

Meet msdn

1.

# Include <windows. h>
# Include <tlhelp32.h>
# Include <stdio. h>

// Forward declarations:
Bool getprocesslist ();
Bool listprocessmodules (DWORD dwpid );
Bool listprocessthreads (DWORD dwownerpid );
Void printerror (tchar * MSG );

Void main ()
{
Getprocesslist ();
}

Bool getprocesslist ()
{
Handle hprocesssnap;
Handle hprocess;
Processentry32 pe32;
DWORD dwpriorityclass;

// Take a snapshot of all processes in the system.
Hprocesssnap = createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (hprocesssnap = invalid_handle_value)
{
Printerror ("createconlhelp32snapshot (of processes )");
Return (false );
}

// Set the size of the structure before using it.
Pe32.dwsize = sizeof (processentry32 );

// Retrieve information about the first process,
// And exit if unsuccessful
If (! Process32first (hprocesssnap, & pe32 ))
{
Printerror ("process32first"); // show cause of failure
Closehandle (hprocesssnap); // must clean up the snapshot object!
Return (false );
}

// Now walk the snapshot of processes, and
// Display information about each process in turn
Do
{
Printf ("/n ==================================== ============================ ");
Printf ("/nprocess name: % s", pe32.szexefile );
Printf ("/n -----------------------------------------------------");

// Retrieve the priority class.
Dwpriorityclass = 0;
Hprocess = OpenProcess (process_all_access, false, pe32.th32processid );
If (hprocess = NULL)
Printerror ("OpenProcess ");
Else
{
Dwpriorityclass = getpriorityclass (hprocess );
If (! Dwpriorityclass)
Printerror ("getpriorityclass ");
Closehandle (hprocess );
}

Printf ("/n process id = 0x % 08x", pe32.th32processid );
Printf ("/n thread count = % d", pe32.cntthreads );
Printf ("/n parent process id = 0x % 08x", pe32.th32parentprocessid );
Printf ("/n priority base = % d", pe32.pcpriclassbase );
If (dwpriorityclass)
Printf ("/n priority class = % d", dwpriorityclass );

// List the modules and threads associated with this process
Listprocessmodules (pe32.th32processid );
Listprocessthreads (pe32.th32processid );

} While (process32next (hprocesssnap, & pe32 ));

Closehandle (hprocesssnap );
Return (true );
}

Bool listprocessmodules (DWORD dwpid)
{
Handle hmodulesnap = invalid_handle_value;
Moduleentry32 me32;

// Take a snapshot of all modules in the specified process.
Hmodulesnap = createconlhelp32snapshot (th32cs_snapmodule, dwpid );
If (hmodulesnap = invalid_handle_value)
{
Printerror ("createconlhelp32snapshot (of modules )");
Return (false );
}

// Set the size of the structure before using it.
Me32.dwsize = sizeof (moduleentry32 );

// Retrieve information about the first module,
// And exit if unsuccessful
If (! Module32first (hmodulesnap, & me32 ))
{
Printerror ("module32first"); // show cause of failure
Closehandle (hmodulesnap); // must clean up the snapshot object!
Return (false );
}

// Now walk the module list of the process,
// And display information about each module
Do
{
Printf ("/n Module name: % s", me32.szmodule );
Printf ("/n executable = % s", me32.szexepath );
Printf ("/n process id = 0x % 08x", me32.th32processid );
Printf ("/n ref count (G) = 0x % 04x", me32.glblcntusage );
Printf ("/n ref count (p) = 0x % 04x", me32.proccntusage );
Printf ("/N base address = 0x % 08x", (DWORD) me32.modbaseaddr );
Printf ("/N base size = % d", me32.modbasesize );

} While (module32next (hmodulesnap, & me32 ));

Closehandle (hmodulesnap );
Return (true );
}

Bool listprocessthreads (DWORD dwownerpid)
{
Handle hthreadsnap = invalid_handle_value;
Threadentry32 te32;

// Take a snapshot of all running threads
Hthreadsnap = createconlhelp32snapshot (th32cs_snapthread, 0 );
If (hthreadsnap = invalid_handle_value)
Return (false );

// Fill in the size of the structure before using it.
Te32.dwsize = sizeof (threadentry32 );

// Retrieve information about the first thread,
// And exit if unsuccessful
If (! Thread32first (hthreadsnap, & te32 ))
{
Printerror ("thread32first"); // show cause of failure
Closehandle (hthreadsnap); // must clean up the snapshot object!
Return (false );
}

// Now walk the thread list of the system,
// And display information about each thread
// Associated with the specified process
Do
{
If (te32.th32ownerprocessid = dwownerpid)
{
Printf ("/n thread id = 0x % 08x", te32.th32threadid );
Printf ("/N base priority = % d", te32.tpbasepri );
Printf ("/n Delta priority = % d", te32.tpdeltapri );
}
} While (thread32next (hthreadsnap, & te32 ));

Closehandle (hthreadsnap );
Return (true );
}

Void printerror (tchar * MSG)
{
DWORD Enum;
Tchar sysmsg [256];
Tchar * P;

Enum = getlasterror ();
Formatmessage (format_message_from_system | format_message_ignore_inserts,
Null, Enum,
Makelangid (lang_neutral, sublang_default), // default language
Sysmsg, 256, null );

// Trim the end of the line and terminate it with a null
P = sysmsg;
While (* P> 31) | (* P = 9 ))
++ P;
Do {* p -- = 0;} while (P> = sysmsg )&&
(* P = '.') | (* P <33 )));

// Display the message
Printf ("/n warning: % s failed with error % d (% s)", MSG, Enum, sysmsg );
}

 

2

#include <windows.h>#include <stdio.h>#include "psapi.h"void PrintModules( DWORD processID ){    HMODULE hMods[1024];    HANDLE hProcess;    DWORD cbNeeded;    unsigned int i;    // Print the process identifier.    printf( "/nProcess ID: %u/n", processID );    // Get a list of all the modules in this process.    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |                                    PROCESS_VM_READ,                                    FALSE, processID );    if (NULL == hProcess)        return;    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))    {        for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )        {            char szModName[MAX_PATH];            // Get the full path to the module's file.            if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,                                      sizeof(szModName)))            {                // Print the module name and handle value.                printf("/t%s (0x%08X)/n", szModName, hMods[i] );            }        }    }    CloseHandle( hProcess );}void main( ){    // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;    unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )        return;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the name of the modules for each process.    for ( i = 0; i < cProcesses; i++ )        PrintModules( aProcesses[i] );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.