//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "windows.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void TForm1::info (AnsiString s) { Memo1->Lines->Append (s); } //--------------------------------------------------------------------------- struct FileName { int d; char name []; }; struct FileData { unsigned int d; char data []; }; void * uloz (void * u, char * jm, int n, char v) { FileName * fn = (FileName *) u; fn->d = strlen (jm); // strcpy (fn->name, jm); memcpy (fn->name, jm, fn->d); FileData * fd = (FileData *) ((char *) u + sizeof (fn->d) + fn->d); fd->d = n; for (int i = 0; i < n; i ++) fd->data [i] = v; return (void *) ((char *) fd + sizeof (fd->d) + fd->d); } void __fastcall TForm1::Button1Click(TObject *Sender) { HANDLE f; f = CreateFile ( "C:\\work\\abc.dat", // pointer to name of the file GENERIC_READ | GENERIC_WRITE, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes CREATE_ALWAYS, // how to create 0, // file attributes NULL // handle to file with attributes to copy ); if (f == INVALID_HANDLE_VALUE) info ("Nejde otevrit"); else { info ("Otevreno"); HANDLE m; m = CreateFileMapping (f,// handle to file to map NULL, // optional security attributes PAGE_READWRITE, // protection for mapping object 0, // high-order 32 bits of object size 16*1024, // low-order 32 bits of object size NULL // name of file-mapping object ); if (m == NULL) info ("Nelze zobrazit"); else { info ("Zobrazeno (1)"); void * p; p = MapViewOfFile (m,// file-mapping object to map into address space FILE_MAP_WRITE, // access mode 0, // high-order 32 bits of file offset 0, // low-order 32 bits of file offset 8*1024 // number of bytes to map ); if (p == NULL) info ("Nelze vytvorit pohled"); else { info ("Zobrazeno (2)"); char * c = (char *) p; c[0] = 'K'; c[1] = 'L'; c[2] = 'M'; strcpy (c+3, "Hello"); void * u = p; u = uloz (u, "PrvniSoubor.txt", 100, 'a'); u = uloz (u, "DruhySoubor.txt", 200, 'b'); UnmapViewOfFile (p); } CloseHandle (m); } CloseHandle (f); } } //---------------------------------------------------------------------------