Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <windows.h>
- #include <tlhelp32.h>
- #include <psapi.h>
- using namespace std;
- BOOL SetPrivilege(
- HANDLE hToken, // access token handle
- LPCTSTR lpszPrivilege, // name of privilege to enable/disable
- BOOL bEnablePrivilege // to enable or disable privilege
- )
- {
- TOKEN_PRIVILEGES tp;
- LUID luid;
- if (!LookupPrivilegeValue(
- NULL, // lookup privilege on local system
- lpszPrivilege, // privilege to lookup
- &luid)) // receives LUID of privilege
- {
- return FALSE;
- }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- if (bEnablePrivilege)
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- else
- tp.Privileges[0].Attributes = 0;
- // Enable the privilege or disable all privileges.
- if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
- {
- return FALSE;
- }
- if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
- {
- return FALSE;
- }
- return TRUE;
- }
- int main()
- {
- HANDLE hProcess;
- HANDLE hToken;
- HANDLE snapshot;
- TCHAR filename[MAX_PATH];
- DWORD charsCarried = MAX_PATH;
- PROCESSENTRY32 process;
- int count = 0;
- snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- process.dwSize = sizeof(PROCESSENTRY32);
- if (snapshot != INVALID_HANDLE_VALUE)
- {
- if (Process32First(snapshot, &process))
- {
- do
- {
- count++;
- OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
- SetPrivilege(hToken, SE_DEBUG_NAME, TRUE);
- hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
- if (hProcess != NULL)
- {
- GetModuleFileNameEx(hProcess, NULL, filename, MAX_PATH);
- printf("%d - PID: %d, Name: %ls, CountThread: %dn",
- count,
- process.th32ProcessID,
- filename,
- process.cntThreads);
- }
- else
- {
- printf("Err: %dn", GetLastError());
- }
- SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
- }
- while (Process32Next(snapshot, &process));
- }
- }
- CloseHandle(hProcess);
- CloseHandle(snapshot);
- return 0;
- }
- #include <stdlib.h>
- #include <locale.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <windows.h>
- #include <tlhelp32.h>
- #include <psapi.h>
- using namespace std;
- void ErrorMes(LPTSTR lpszFunction)
- {
- // Retrieve the system error message for the last-error code
- LPVOID lpMsgBuf;
- LPVOID lpDisplayBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0, NULL );
- // Display the error message
- lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
- (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
- wprintf(L"%s failed with error %d: %s",
- lpszFunction, dw, lpMsgBuf);
- LocalFree(lpMsgBuf);
- LocalFree(lpDisplayBuf);
- }
- int main()
- {
- HANDLE hProcess=NULL;
- HANDLE hToken;
- HANDLE snapshot;
- TCHAR filename[MAX_PATH];
- DWORD charsCarried = MAX_PATH;
- PROCESSENTRY32 process;
- int count = 0;
- setlocale(LC_ALL,"Russian");
- snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- process.dwSize = sizeof(PROCESSENTRY32);
- if (snapshot != INVALID_HANDLE_VALUE)
- {
- if (Process32First(snapshot, &process))
- {
- do
- {
- wprintf(L"PID: %d, Name: %s, CountThreads: %dn",
- process.th32ProcessID,
- process.szExeFile,
- process.cntThreads);
- /*get process handle*/
- hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
- if(hProcess==NULL){
- ErrorMes(L"OpenProcess");
- wprintf(L"n");
- continue;
- }
- charsCarried=MAX_PATH;
- /* get executable name*/
- if(QueryFullProcessImageName(hProcess,0,filename,&charsCarried)!=FALSE)
- {
- wprintf(L"%sn",
- filename);
- }
- else
- {
- ErrorMes(L"QueryFullProcessImageName");
- wprintf(L"n");
- }
- wprintf(L"n");
- CloseHandle(hProcess);
- hProcess = NULL;
- }
- while (Process32Next(snapshot, &process));
- }
- }
- else
- {
- ErrorMes(L"CreateToolhelp32Snapshot");
- }
- if(hProcess!=NULL)CloseHandle(hProcess);
- CloseHandle(snapshot);
- system("PAUSE");
- return 0;
- }
Add Comment
Please, Sign In to add comment