CreateFileMapping MapViewOfFile
With win32api, I want that the following program creates two process and creates a filemap. (using c++)
i don't know what i should write at Handle CreateFileMapping(...
.
I've tried it with:
PROCCESS_INFORMATION hfile.
Furthermore the first parameter should be INVALID_HANDLE_VALUE
, but then i don't know what to write into MapViewOfFile
as first parameter.
the code from the first program: (i didn't programmed the 2.&3. because even the first doesn't work)
//Initial process creates proccess 2 and 3
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void main()
{
bool ret;
bool retwait;
bool bhandleclose;
STARTUPINFO startupinfo;
GetStartupInfo (&startupinfo);
PROCESS_INFORMATION pro2info;
PROCESS_INFORMATION pro3info;
//create proccess 2
wchar_t wcsCommandLine[] = L"D:\\betriebssystemePRA2pro2.exe";
ret = CreateProcess(NULL, wcsCommandLine, NULL, NULL, false, CREATE_NEW_CONSOLE, NULL,
NULL, &startupinfo, &pro2info);
if (ret==false){
cout<<"Prozess konnte nicht erzeugt werden. Fehler:"<<GetLastError();
ExitProcess(0);
}
//***************
//create process3
wchar_t wcs2CommandLine[] = L"D:\\betriebssystemePRA2pro3.exe";
ret = CreateProcess(NULL, wcs2CommandLine, NULL, NULL, false, CREATE_NEW_CONSOLE, NULL,
NULL, &startupinfo, &pro3info);
if (ret==false){
cout<<"Prozess konnte nicht erzeugt werden. Fehler:"<<GetLastError();
ExitProcess(0);
}
//***************
//create mapping object
// program2:
PROCESS_INFORMATION hfile;
CreateFileMapping( //erzeugt filemapping obj returned ein handle
INVALID_HANDLE_VALUE, //mit dem handle-->kein 开发者_如何学JAVAseperates file nötig
NULL,
PAGE_READWRITE, //rechte (lesen&schreiben)
0,
5,
L"myfile"); //systemweit bekannter name
LPVOID mappointer = MapViewOfFile( //virtuelle speicherraum, return :zeiger, der auf den bereich zeigt
INVALID_HANDLE_VALUE, //handle des filemappingobj.
FILE_MAP_ALL_ACCESS,
0,
0,
100);
//wait
cout<<"beliebige Taste druecken"<<endl;
cin.get();
//close
bool unmap;
unmap = UnmapViewOfFile (mappointer);
if (unmap==true)
cout<<"Unmap erfolgreich"<<endl;
else
cout<<"Unmap nicht erfolgreich"<<endl;
bhandleclose=CloseHandle (INVALID_HANDLE_VALUE);
cout<<bhandleclose<<endl;
bhandleclose=CloseHandle (pro2info.hProcess);
bhandleclose=CloseHandle (pro3info.hProcess);
ExitProcess(0);
}
MapViewOfFile takes the handle returned by CreateFileMapping:
HANDLE hFileMapping = CreateFileMapping(...);
LPVOID lpBaseAddress = MapViewOfFile(hFileMapping, ...);
You need to pass the return value of CreateFileMapping
as the first parameter of MapViewOfFile
. Also, the number of bytes to map in MapViewOfFile
should be small enough that the view isn't longer than the file itself.
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, 5, L"myfile");
LPVOID mappointer = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 5);
精彩评论