![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Then we start that program again as a new process. ---------------------------------------------- | | |---------------------------------------------- | | --------------------------- | | |Here's the data... | | | | ------------------------------------------ | | | | | | | |------------------------------------------- | | | | --------------------------- | | | | | | | | | | | | | | ------------- | | | | | | | | | ---------------- | | | | | | | | | | | | | --------------------------- | | | --------------------------------------- Next, we read the data in from the memory file in the second process. ---------------------------------------------- | | |---------------------------------------------- | | --------------------------- | | |Here's the data... | | | | ---------------------------------------------- | | | | | | |---------------------------------------------- | | | | --------------------------- | | | | |Here's the data... | | | | | | | | | -------------- | | | | | | | | | --------------- | | | | | | | | | | | | | --------------------------- | | | --------------------------------------------
In this way, we are able to communicate between running processes. Create this new project, ShareMem, now. Create a new MDI program named ShareMem with the AppWizard. The first step in writing ShareMem will be to let the user type in the data they want to transfer from process to process, and we support that now. Getting the Data to TransferTo read keys from the keyboard and store them in a CString in the document named, for example, text, add an OnChar() function and place the following code in it: void CShareMemView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { CShareMemDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDoc->text += nChar; Invalidate(); CView::OnChar(nChar, nRepCnt, nFlags); } To display that text, we add this code to OnDraw(): void CShareMemView::OnDraw(CDC* pDC) { CShareMemDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDC->TextOut(0, 0, pDoc->text); ⇐ } Now we have the data we want to place in the memory-mapped file and have stored it in a CString object named text. The next step is to write that data to a memory-mapped file and then read it in. To let the user do that, add two more items to the File menu: Write Memory File and Read Memory File. All we have to do now is to make these new menu items work. Writing a Memory-Mapped FileFirst we write our data to a memory-mapped file in OnFileWritememoryfile(). We begin by getting a pointer to the document. void CShareMemView::OnFileWritememoryfile() { CShareMemDoc* pDoc = GetDocument(); ⇐ ASSERT_VALID(pDoc); ⇐ . . . To create a memory-mapped file, we use CreateFileMapping(). HANDLE CreateFileMapping(HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpName); Here, we pass it a file handle (we use 0xFFFFFFFF to indicate that we want to use the systems own swap file for our file); a pointer to a SECURITY_ATTRIBUTES structure, which we leave NULL; the file attributes, which can include:
followed by the high double word of the file size were requesting, the low double word of the file size, and the name of the file were creating, which we call MemoryFile. void CShareMemView::OnFileWritememoryfile() { CShareMemDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); HANDLE MemoryMappingHandle; ⇐ MemoryMappingHandle = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL, PAGE_READWRITE, 0, 2048, MemoryFile); ⇐ . . . Now weve created our memory-mapped file. To use it, we need a view of that filemuch like the views were already familiar with. Creating Memory File ViewsWe use MapViewOfFile() to get an actual pointer to our new file in memory. LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, DWORD dwNumberOfBytesToMap); MapViewOfFile() returns a pointer to the start of our file in memory. The first parameter is a handle of a open file-mapping object. The next parameter specifies the type of access we want and can take the following values:
The next two parameters hold the offset where mapping is to begin in the file and the last parameter holds the number of bytes to map. If the last parameter is zero, we map the entire file. void CShareMemView::OnFileWritememoryfile() { CShareMemDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); HANDLE MemoryMappingHandle; MemoryMappingHandle = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL, PAGE_READWRITE, 0, 2048, MemoryFile); LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_WRITE, 0, 0, 0); ⇐ . . .
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |