file mapping



  • int main()
    {  
    	HANDLE h_File;
             HANDLE h_FileMapping;
             LPVOID lpv_FileMappingPointer;
    
    	// File erzeugen
    	h_File = CreateFile("C:\\Werte.txt", 
    				GENERIC_READ | GENERIC_WRITE, 
    				0,
    				NULL,
    				OPEN_EXISTING,
    				FILE_ATTRIBUTE_NORMAL,
    				NULL);
    
    	if (h_File == INVALID_HANDLE_VALUE) 
    	{
    	      MessageBox(NULL, "Can not open file", "Error", 0);
                   return 0;
    	}
    
             // FileMapping erzeugen
    	h_FileMapping = CreateFileMapping(h_File,
    				NULL,
    				PAGE_READWRITE,
    				0,
    				0,
    				"FileMapping");
    
    	if (h_FileMapping == INVALID_HANDLE_VALUE) 
             { 
                if (h_File != INVALID_HANDLE_VALUE) 
    		{
    			MessageBox(NULL, "Can not open file mapping", "Error", 0);
                CloseHandle(h_File); 
    		}
            return 0; 
        }
    
    	lpv_FileMappingPointer = MapViewOfFile(h_FileMapping,
    				FILE_MAP_ALL_ACCESS,
    				0,
    				0,
    				0);
    
       //static_cast<int>(* static_cast<int*>(lpv_FileMappingPointer)) = 1;
       *(int*)lpv_FileMappingPointer=1;
    
       UnmapViewOfFile(lpv_FileMappingPointer);
       CloseHandle(h_FileMapping);
       CloseHandle(h_File);
    
       return 0;
    }
    

    hi leute!

    warum funktioniert das nicht?

    Unhandled exception at 0x0041c4d8 in Client.exe: 0xC0000005: Access violation writing location 0x00000000.
    

    hab mal debuggt...h_FileMapping hat einen ungültigen handle...hm ka warum!??

    cu



  • Ist lpv_FileMappingPointer vielleicht ein NULL-Pointer?



  • LPVOID lpv_FileMappingPointer;
    ist ja ein long pointer vom typ void!

    cu



  • Was soll uns das jetzt sagen? MapViewOfFile kann einen NULL-Pointer zurückgeben.



  • blub_ schrieb:

    Was soll uns das jetzt sagen? MapViewOfFile kann einen NULL-Pointer zurückgeben.

    ich lese grad:
    CreateFileMapping returns another handle that you have to save, or NULL if failed.

    warum ich einen NULL pointer bekomme hab ich keinen schimmer;-(

    cu



  • if(h_FileMapping == NULL)
    	{
            LPVOID lpMsgBuf; 
            FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
                                            FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 
                                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); 
            MessageBox (NULL, (LPCTSTR)lpMsgBuf, "Error: CreateFile", MB_OK | MB_ICONINFORMATION); 
            LocalFree (lpMsgBuf);
    	}
    

    Gibt mir folgende Fehlermeldung aus:
    Das Handle ist ungültig;-((

    hm



  • Hier ein Ausschnitt aus der Dokumentation zu CreateFileMapping():

    dwMaximumSizeLow
    Low-order DWORD of the maximum size of the file mapping object. If this parameter and dwMaximumSizeHigh are zero, the maximum size of the file mapping object is equal to the current size of the file identified by hFile.
    An attempt to map a file with a length of zero in this manner fails with an error code of ERROR_FILE_INVALID. Applications should test for files with a length of zero and reject such files.

    Könnte das vielleicht das Problem sein?
    Wenn deine Datei wirklich eine Größe von 0 Bytes sein, dann muß du die gewünschte Größe des Mappings angeben anstatt (mit dem Wert 0) automatisch das ganze File zu mappen.

    - Raving Tux



  • ja könnte sein...denn ich erstelle die datei erst, also die datei existiert noch nicht...
    #OPEN_ALWAYS: create a new file if it doesn't exist, or open the old if it exists.

    gewünschte Größe des Mappings? hab da 1 genommen...und es geht;-) wie interpretiert man nun die 1????

    cu



  • nike_ schrieb:

    gewünschte Größe des Mappings? hab da 1 genommen...und es geht;-) wie interpretiert man nun die 1????

    Nan ja, wie in der Doku zu CreateFileMapping() nachzulesen ist, ist das die maximale Größe des Mappings, d.h. du kannst mit MapViewOfFile keine größere View mappen.

    Du solltest dir die Doku zu CreateFileMapping und MapViewOfFile (noch) mal genau durchlesen. Da sollte eigentlich alles drinstehen.

    - Raving Tux



  • hi!
    Ich will das File Mapping der Datei in 3 Teile aufteilen...
    File file(fileName);
    FileMapping map(file, SEGMENTSIZE);
    lpv_FileMappPtr=MapViewOfFile(map1.getHandle(),FILE_MAP_ALL_ACCESS,0,0,0);

    brauch ich dann 3 mal FileMapping? dann wird die datei 3 mal grösser...ok
    und 3 mal das File Mapping mit MapViewOfFile auflisten?

    ist das ok?

    in das File mappen:
    -------------------

    void* lpv_FileMappPtr1;

    File file(fileName); // Instanz der Klasse File erstellen...mit CreateFile datei erstellen...
    FileMapping map(file, SEGMENTSIZE);

    lpv_FileMappPtr=MapViewOfFile(map1.getHandle(),FILE_MAP_ALL_ACCESS,0,0,0);

    * static_cast<int*>(lpv_FileMappPtr) = 12; // Wert 12 reinschreiben...

    schreibt man sonst auch so in die datei? //SetFilePointer...nimmt man auch noch..ok

    cu


Anmelden zum Antworten