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