views:

218

answers:

2

hello i'm programming with win32api, i want that the following programm creates two process and creates an filemap. (using c++)

i don't know what i should write at Handle CreateFileMapping(... i 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 programm: (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 seperates 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);


}
+1  A: 

MapViewOfFile takes the handle returned by CreateFileMapping:

HANDLE hFileMapping = CreateFileMapping(...);
LPVOID lpBaseAddress = MapViewOfFile(hFileMapping, ...);
Luke
A: 

You need to pass the return value of CreateFileMapping at 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);
Adrian McCarthy