我制作了2个程序,你可以在下面找到。
第一个计算所有具有< 3个线程并在映射文件中写入其名称的进程。
第二个在第一个主函数中打开,应该显示它在映射文件中找到的内容。然而,它没有,我认为这是因为没有写入内容,但我似乎无法弄清楚原因。
首先:
#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();
}
第二
/*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;
}
答案 0 :(得分:1)
下班后的代码:
#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;
int main(void)
{ //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";
}
//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);
}
//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)
{ // sprintf(pids,"%d",proc32.th32ProcessID);
lstrcat(names, proc32.szExeFile);
lstrcat(names, L"\n");
}
}
}while(Process32Next(hSnap, &proc32));//cat timp mai sunt procese in snapshot
//inchid handle la snapshot
CloseHandle(hSnap);
//afisez string-ul de procese
wcout<<names;
//scriu in fisierul mapat
HANDLE hMapFile;
LPCTSTR pBuf;
TCHAR szName[]=TEXT("Global\\mapFile");
//il creez
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 1;
}
//fac fisierul utilizabil
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 1;
}
//scriu in el
CopyMemory((PVOID)pBuf, names, (_tcslen(names) * sizeof(TCHAR)));
//deschid al doilea proces care va citi din fisier si inchide procesele
STARTUPINFO startupinfo ;
PROCESS_INFORMATION pinfo ;
ZeroMemory( &startupinfo, sizeof(startupinfo) );
startupinfo.cb = sizeof(startupinfo);
ZeroMemory( &pinfo, sizeof(pinfo) );
//creez noul proces
if(!CreateProcess(L"b.exe", NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS,
NULL, NULL, &startupinfo, &pinfo))
{
_tprintf(TEXT("Eroare la create process (%d).\n"),
GetLastError());
return 1;
}
// blochez A pana se termina B
WaitForSingleObject(pinfo.hProcess, INFINITE);
//unmap
UnmapViewOfFile(pBuf);
//inchid handle la map
CloseHandle(hMapFile);
getchar();
}