C + + Get process information list and DLL list called by Process _c language

Source: Internet
Author: User


Copy Code code as follows:

-----------------------//FileName:
ProcessInfo.h
Remarks:
Based on the application layer implementation, some processes, such as the kill soft process, etc. get the list of DLLs that are not called.
// -----------------------

#pragma once
#include <vector>

struct PROINFO
{
Save Process PID
unsigned int upid;
Save Process Name
CString Strprcename;
Save Process Path
CString strFullPath;
Save the process call DLL name and path
Std::vector<cstring> Strdllnamearr;
};

Class Cprocessinfo
{
Private
This is for the right to claim.
BOOL Enabledebugprivilege (bool fenable);
Public
Save Process Name
Std::vector<proinfo> Strprceinfoarr;

Cprocessinfo ();
~cprocessinfo ();

Get Process Name
void Getprocessname (void);
};

Copy Code code as follows:

// ------------------------------------------------------------------------------------------------------------- -----------
FileName:
ProcessInfo.cpp
Remarks:
Based on the application layer implementation, some processes, such as the kill soft process, etc. get the list of DLLs that are not called.
// ------------------------------------------------------------------------------------------------------------- -----------

#include "stdafx.h"
#include "ProcessInfo.h"
#include "TlHelp32.h"
#include "StrSafe.h"
#include "Psapi.h"
Prevent error LNK2019
#pragma comment (lib, "PSAPI.lib")

Cprocessinfo::cprocessinfo ()
{

}

Cprocessinfo::~cprocessinfo ()
{

}

BOOL Cprocessinfo::enabledebugprivilege (bool fenable)
{
BOOL fOk = FALSE;
HANDLE Htoken;

Get access token for process
if (OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges,&htoken))
{
Token_privileges TP;
Tp. Privilegecount = 1;
View system privilege values and return a LUID structure
Lookupprivilegevalue (NULL, Se_debug_name, &AMP;TP. Privileges[0]. LUID);
Tp. Privileges[0]. Attributes = fenable? se_privilege_enabled:0;
Enable/Turn off privileges
AdjustTokenPrivileges (Htoken, FALSE, &AMP;TP, sizeof (TP), NULL, NULL);
FOk = (GetLastError () = = ERROR_SUCCESS);
CloseHandle (Htoken);
}
Else
{
return 0;
}
return (FOK);
}

void Cprocessinfo::getprocessname (void)
{
HANDLE hprocesssnap = NULL;
HANDLE Hprocessdll = NULL;
BOOL bRet = FALSE;
Initialize dwsize to 0, or Process32First execution fails
PROCESSENTRY32 pe32 = {0};
MODULEENTRY32 me32;
LPVOID lpMsgBuf;
LPVOID Lpdisplaybuf;
DWORD dwerror;
Proinfo Proinfo;
LPCTSTR Pszformat = TEXT ("encountered an error while starting the service!") %s ");

Create a process Snapshot

if (! Enabledebugprivilege (1))
{
MessageBox (NULL, _t ("Power to claim failure!") "), _t (" hint "), mb_ok| Mb_iconexclamation);
}

Hprocesssnap = CreateToolhelp32Snapshot (th32cs_snapprocess, 0);

if (Hprocesssnap = = INVALID_HANDLE_VALUE)
{
dwerror = GetLastError ();
FormatMessage (
format_message_allocate_buffer|
format_message_from_system|
Format_message_ignore_inserts,
Null
Dwerror,
Makelangid (Lang_neutral, Sublang_default),
LPTSTR (&AMP;LPMSGBUF),
0,
NULL);

Lpdisplaybuf = (LPVOID) LocalAlloc (
Lmem_zeroinit,
(Lstrlen ((LPCTSTR) lpmsgbuf) +lstrlen (Pszformat)) *sizeof (TCHAR));

format string
stringcchprintf (
(LPTSTR) Lpdisplaybuf,
Localsize (LPDISPLAYBUF),//number of bytes
Pszformat,
LPMSGBUF);

CString strtemp;
Strtemp.format (TEXT ("Error code:%d"), dwerror);
:: MessageBox (NULL, LPCTSTR) lpdisplaybuf, strtemp, mb_ok| Mb_iconexclamation);
To clean up allocated memory
LocalFree (LPMSGBUF);
LocalFree (LPDISPLAYBUF);

Return
}

pe32.dwsize = sizeof (PROCESSENTRY32);

Module32first (Hprocesssnap, &me32);

if (Process32First (Hprocesssnap, &pe32))
{
Todo
{
WCHAR path[max_path]={0};

Proinfo.upid = Pe32.th32processid;
Proinfo.strprcename = Pe32.szexefile;

Hmodule hmodule;
HANDLE hprocess;
DWORD needed;
Hprocess=openprocess (Process_query_information | Process_vm_read, False, Pe32.th32processid);
if (hprocess)
{
Enumerating processes
EnumProcessModules (hprocess, &hmodule, sizeof (hmodule), &needed);
Get the full path of a process
Getmodulefilenameex (hprocess, hmodule, path, sizeof (path));
Save path
Proinfo.strfullpath = path;
}
Else
{
Proinfo.strfullpath = _t ("Unable to get process path");
}
Strprceinfoarr.push_back (Proinfo);
}
while (Process32Next (Hprocesssnap, &pe32));
}

Std::vector<proinfo>::iterator ITER;
for (iter = Strprceinfoarr.begin (); Iter!= strprceinfoarr.end (); iter++)
{
Get a snapshot of this process
Hprocessdll = CreateToolhelp32Snapshot (Th32cs_snapmodule, iter->upid);
me32.dwsize = sizeof (MODULEENTRY32);
if (! Module32first (Hprocessdll, &me32) | | iter->upid==0)
{
Continue
}
Todo
{
Iter->strdllnamearr.push_back (Me32.szexepath);
}
while (Module32next (Hprocessdll, &me32));
}

Turn off privileges
Enabledebugprivilege (0);
Close Kernel Object
CloseHandle (HPROCESSSNAP);
}

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.