I made 2 programs which you can find below.
The first one counts all the processes that have <3 threads and writes their name in a mapped file.
The second one is opened in the first's main function and should display what it finds in the mapped file. However, it doesn't and I think it's because nothing is written in it, but I can't seem to figure out why.
First:
#include "stdafx.h"
#include <windows.h>
#include <Tlhelp32.h>
#include <iostream>
#include <list>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
// http://www.cplusplus.com/forum/articles/16820/
// http://stackoverflow.com/questions/3298569/difference-between-mbcs-and-utf-8-on-windows
// http://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc
using namespace std;
void writeToFileMap(LPCTSTR msg)
{
HANDLE hMapFile;
LPCTSTR pBuf;
TCHAR szName[]=TEXT("mapFile");
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
256, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return ;
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
256);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return ;
}
CopyMemory((PVOID)pBuf, msg, (_tcslen(msg) * sizeof(LPCTSTR)));
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
}
}
void getProcessList()
{//snapshot la toate procesele din sistem
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 proc32;
TCHAR names[MAX_PATH]=L""; //wchar_t pentru ca folosim Unicode
if(hSnap == INVALID_HANDLE_VALUE)
{
cout<<"invalid handle value error!\n";
return;
}
//setez dimensiunea structurii
proc32.dwSize = sizeof(PROCESSENTRY32);
//get info despre primul proces(se va afisa in do...while)
if(!Process32First(hSnap, &proc32))
{
cout<<"Tread32First() error!\n";
CloseHandle(hSnap);
return ;
}
//cauta in restul proceselor
//daca nr. threaduri<3, introdu in fisierul mapat
do
{
if(proc32.cntThreads < 3)
{
//cout<<"Current process id(adica programul A): "<<GetCurrentProcessId()<<"\n";
wcout<<L"Process Name: "<<proc32.szExeFile<<"\n";
cout<<"Process ID: " <<proc32.th32ProcessID<<"\n";
cout<<"Thread Count: "<<proc32.cntThreads<<"\n"<<endl;
//exclud procesul curent, nu vreau sa-l termin
//includ celelalte procese in string, separate de newline
if(GetCurrentProcessId()!=proc32.th32ProcessID)
{
lstrcat(names, proc32.szExeFile);
lstrcat(names, L"\n");
}
}
}while(Process32Next(hSnap, &proc32));
//afisez
wcout<<names;
//scriu in fisierul mapat
writeToFileMap(names);
//inchid handle la snapshot
CloseHandle(hSnap);
}
int main(void)
{
//scriu in fisierul mapat procesele
getProcessList();
//deschid al doilea proces care va citi din fisier si inchide procesele
STARTUPINFO startupinfo ;
startupinfo.cb = sizeof (startupinfo) ;
PROCESS_INFORMATION pinfo ;
memset(&startupinfo, 0, sizeof (startupinfo)) ;
if(!CreateProcess(L"Tema2CSSO.exe", NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS,
NULL, NULL, &startupinfo, &pinfo))
{
_tprintf(TEXT("Eroare la create process (%d).\n"),
GetLastError());
}
// Wait until application has terminated
WaitForSingleObject(pinfo.hProcess, INFINITE);
getchar();
}
Second:
/*Creati 2 programe:
1. Primul va enumera toate procesele din sistem care au mai putin de 3 fire de executie si le va
transmite, printr-un fisier mapat in memorie, programului 2
2. Al doilea program, la initializare, isi va seta privilegiul SE_DEBUG_NAME si va omori toate
procesele transmise de programul 1.
*/
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#define BUF_SIZE 256
TCHAR szName[]=TEXT("mapFile");
int main()
{
HANDLE hMapFile;
LPCTSTR pBuf;
//deschid fisierul mapat
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
std::cout<<"Could not open file mapping object.\n";
return 1;
}
//asociez un handle
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
std::cout<<"Could not map view of file.\n";
CloseHandle(hMapFile);
return 1;
}
//inchid procesele
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}