PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSFileMappingClient/ReadMe.txt

#
Plain Text | 108 lines | 77 code | 31 blank | 0 comment | 0 complexity | 5a1069bcac0f70631b56a8896e3242a3 MD5 | raw file
  1========================================================================
  2    CONSOLE APPLICATION : CSFileMappingClient Project Overview
  3========================================================================
  4
  5/////////////////////////////////////////////////////////////////////////////
  6Summary:
  7
  8File mapping is a mechanism for one-way or duplex inter-process communication 
  9among two or more processes in the local machine. To share a file or memory, 
 10all of the processes must use the name or the handle of the same file mapping 
 11object.
 12
 13To share a file, the first process creates or opens a file by using the 
 14CreateFile function. Next, it creates a file mapping object by using the 
 15CreateFileMapping function, specifying the file handle and a name for the 
 16file mapping object. The names of event, semaphore, mutex, waitable timer, 
 17job, and file mapping objects share the same name space. Therefore, the 
 18CreateFileMapping and OpenFileMapping functions fail if they specify a name
 19that is in use by an object of another type.
 20
 21To share memory that is not associated with a file, a process must use the 
 22CreateFileMapping function and specify INVALID_HANDLE_VALUE as the hFile 
 23parameter instead of an existing file handle. The corresponding file mapping 
 24object accesses memory backed by the system paging file. You must specify 
 25a size greater than zero when you use an hFile of INVALID_HANDLE_VALUE in a 
 26call to CreateFileMapping.
 27
 28Processes that share files or memory must create file views by using the 
 29MapViewOfFile or MapViewOfFileEx function. They must coordinate their access 
 30using semaphores, mutexes, events, or some other mutual exclusion technique.
 31
 32The VC# code sample demonstrates opening a file mapping object named 
 33"Local\SampleMap" and reading the string written to the file mapping by other 
 34process. Because the Base Class Library of .NET Framework 2/3/3.5 does not 
 35have any public classes to operate on file mapping objects, you have to 
 36P/Invoke the Windows APIs as shown in this code sample.
 37
 38
 39/////////////////////////////////////////////////////////////////////////////
 40Demo:
 41
 42The following steps walk through a demonstration of the file mapping sample.
 43
 44Step1. After you successfully build the CSFileMappingClient and 
 45CSFileMappingServer sample projects in Visual Studio 2008, you will get the 
 46applications: CSFileMappingClient.exe and CSFileMappingServer.exe. 
 47
 48Step2. Run CSFileMappingServer.exe in a command prompt. The application will 
 49create a file mapping object of a specified size that is backed by the system 
 50paging file. Its name is "Local\SampleMap".
 51
 52  The file mapping (Local\SampleMap) is created
 53
 54Next, the application maps a view of the file mapping into the address space 
 55of the process, and writes a string to the view.
 56
 57  The file view is mapped
 58  This message is written to the view:
 59  "Message from the first process."
 60
 61Step3. Run CSFileMappingClient.exe in another command prompt. 
 62CSFileMappingClient opens the file mapping object "Local\SampleMap", maps 
 63the same view of the file mapping into its address space, and read the string 
 64written by the first process from the view.
 65
 66  The file mapping (Local\SampleMap) is opened
 67  The file view is mapped
 68  Read from the file mapping:
 69  "Message from the first process."
 70
 71Step4. Press ENTER in both command prompts to close CSFileMappingServer and 
 72CSFileMappingClient.
 73
 74
 75/////////////////////////////////////////////////////////////////////////////
 76Sample Relation:
 77(The relationship between the current sample and the rest samples in 
 78Microsoft All-In-One Code Framework http://1code.codeplex.com)
 79
 80CSFileMappingClient -> CSFileMappingServer
 81CSFileMappingServer creates the file mapping named "Local\SampleMap" and 
 82writes a string to it. CSFileMappingClient reads the string from the file 
 83mapping.
 84
 85
 86/////////////////////////////////////////////////////////////////////////////
 87Code Logic:
 88
 891. Try to open the file mapping object "Local\SampleMap" by P/Invoking 
 90OpenFileMapping.
 91
 922. Map a view of the file mapping into the address space of the current 
 93process by P/Invoking MapViewOfFile.
 94
 953. Read a string from the view.
 96
 974. Unmap the file view (UnmapViewOfFile) and close the file mapping object 
 98(CloseHandle).
 99
100
101/////////////////////////////////////////////////////////////////////////////
102References:
103
104MSDN: Creating Named Shared Memory
105http://msdn.microsoft.com/en-us/library/aa366551.aspx
106
107
108/////////////////////////////////////////////////////////////////////////////