2010-04-12 43 views
0

使用win32api,我希望以下程序創建兩個進程並創建一個filemap。 (使用c + +)CreateFileMapping MapViewOfFile

我不知道我應該寫在Handle CreateFileMapping(...。 我已經嘗試過:

PROCCESS_INFORMATION hfile. 

此外,第一個參數是INVALID_HANDLE_VALUE,但後來我不知道該怎麼寫進去MapViewOfFile作爲第一個參數。

從第一程序代碼:(我沒有編程的2 & 3,因爲即使是第一次不工作)

//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); 


} 

回答

2

MapViewOfFile需要返回的句柄通過的CreateFileMapping:

HANDLE hFileMapping = CreateFileMapping(...); 
LPVOID lpBaseAddress = MapViewOfFile(hFileMapping, ...); 
1

您需要傳遞返回值CreateFileMapping作爲MapViewOfFile的第一個參數。而且,MapViewOfFile中映射的字節數應該足夠小,以使視圖不超過文件本身。

HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 
            0, 5, L"myfile"); 

LPVOID mappointer = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 5); 
相關問題