【发布时间】:2014-04-24 04:38:51
【问题描述】:
我制作了 2 个程序,您可以在下面找到它们。
第一个计算具有
第二个在第一个的主函数中打开,应该显示它在映射文件中找到的内容。但是,它没有,我认为这是因为里面没有写任何东西,但我似乎无法弄清楚为什么。
第一:
#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;
}
【问题讨论】:
-
第一个进程在您启动第二个进程之前关闭 MMF 句柄 - 当它尝试读取它时,它已经消失了。
-
在不关闭手柄的情况下运行它仍然不打印。
-
@GeorgeIrimiciuc 如果第一个程序在没有关闭句柄的情况下终止,Windows 会为您关闭它。尝试在第一个 prog 的 CopyMemory 之后添加
Sleep( INFINITE )。 -
决定将所有内容添加到我的主要功能中并且它有效。不过,不使用函数很烦人。
标签: c++ winapi process memory-mapped-files