Process communication via pagefile
>> Sunday, October 18, 2009
We have 2 process, one is to create the file, and the other one is to read the file.
The tip i want to show here is using function of mapped file.
We need 2 functions to do the thing.
Yes, this function really creates a mapping object, who will map our normal file to the memory. We need it with the second function:HANDLE CreateFileMapping(HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttr, DWORD
flProtect, DWORD maximum_size_high, DWORD maximum_size_low, LPCTSTR lpName);
where hFile - file handle lpAttr - security attributes flProtect - flags of
file access maximum_size_ * - size of object lpName - name of mapping object
To understand the functions, let 's see the sample code.LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD desired_access, DWORD
offset_high, DWORD offset_low, SIZE_T number_of_bytes_to_map);
#include <windows.h>
#include <fstream>
using namespace std;
int main()
{
int a[10] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
char file_name[] = "demo.bin";
HANDLE hFile, hMapping;
int *ptr; // pointer to a
ofstream out(file_name, ios: out | ios::binary);
for (int i = 0; i < 10; i++)
out.write((char*) &a[i], sizeof(int));
out.close();
hFile = CreateFile(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
ptr = (int*)MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
// here we map file from beginning and map all the file.
// modify the array
for (int i = 0; i < 10; ++i)
ptr[i] += 10;
UnmapViewOfFile(ptr);
CloseHandle(hMapping);
CloseHandle(hFile);
}
The process here is:
[File on disk] --> [View in memory] --> [Modified File on disk]
You don't need to due complexly with direct operations on the file. Now with these 2 functions, we have a cool tool to work with files like variables and pointers.
The last example now is the process communication using mapped file.
Suppose we had the file demo.bin on disk.
The above code doesn't have a mapping object name. Now we need a name for the mapping object, and this object will be shared among processes !!!
I just recommend some modified codes here:
Suppose now we have a file "C:\\ReadArray.exe" , which reads the array through file mapping.char mapping_name[] = "MappingName";
// now create the mapping object with this name
// use INVALID_HANDLE_VALUE indicates that we use the pagefile of operating system
hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, n
*sizeof(int), < b > mapping_name < / b > );
Now is the code for loading the reading process:
char lpszAppName[] = "C:\\ReadArray.exe";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
// spawn new console process
CreateProcess(lpszAppName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL,
NULL, &si, &pi);
// just wait now...
WaitForSingleObject(pi.hProcess, NIFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
0 comments:
Post a Comment