Click here to Skip to main content
15,920,508 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralException: Async Vs Sync Pin
Robin7-Sep-00 18:24
Robin7-Sep-00 18:24 
GeneralSize of property sheet/page Pin
Eq7-Sep-00 17:09
Eq7-Sep-00 17:09 
GeneralRe: Size of property sheet/page Pin
Paolo Messina8-Sep-00 8:00
professionalPaolo Messina8-Sep-00 8:00 
GeneralAdding toolbars to a dockable window Pin
Shekhar7-Sep-00 12:42
Shekhar7-Sep-00 12:42 
GeneralPassing strings between running apps Pin
#realJSOP7-Sep-00 11:50
professional#realJSOP7-Sep-00 11:50 
GeneralRe: Passing strings between running apps Pin
Michael Dunn7-Sep-00 14:33
sitebuilderMichael Dunn7-Sep-00 14:33 
GeneralRe: Passing strings between running apps Pin
Feng Yuan (www.fengyuan.com)7-Sep-00 18:20
sussFeng Yuan (www.fengyuan.com)7-Sep-00 18:20 
GeneralRe: Passing strings between running apps Pin
Mustafa Demirhan8-Sep-00 13:31
Mustafa Demirhan8-Sep-00 13:31 
The following lines are taken from 'Visual C++ 4 Unleashed":

***********************************************************
Memory-Mapped Files and Shared Memory
---------------------------------------
Earlier in this chapter, I mentioned that applications are no longer capable of communicating using global memory created with the GMEM_DDESHARE flag. Instead, they must use memory-mapped files to share memory. What are memory-mapped files?

Normally, the virtual memory mechanism enables an operating system to map nonexistent memory to a disk file, called the paging file. It is possible to look at this the other way around and see the virtual memory mechanism as a method of referring to the contents of a file, namely the paging file, through pointers as if the paging file were a memory object. In other words, the mechanism maps the contents of the paging file to memory addresses. If this can be done with the paging file, why not with other files? Memory-mapped files represent this natural extension to the virtual memory management mechanism.

You can create a file mapping by using the CreateFileMapping function. You can also use the OpenFileMapping function to enable an application to open an existing named mapping. The MapViewOfFile function maps a portion of the file to a block of virtual memory.

The special thing about memory-mapped files is that they are shared between applications. That is, if two applications open the same named file mapping, they will, in effect, create a block of shared memory.

Isn't it a bit of an overkill to be forced to use a disk file when the objective is merely to share a few bytes between two applications? Actually, it is not necessary to explicitly open and use a disk file in order to obtain a mapping in memory. Applications can submit the special handle value of 0xFFFFFFFF to CreateFileMapping in order to obtain a mapping to the system paging file itself. This, in effect, creates a block of shared memory.

Listings 13.3 and 13.4 demonstrate the use of shared memory objects for intertask communication. They implement a very simple mechanism where one program, the client, deposits a simple message (a null-terminated string) in shared memory for the other program. This other program, the server, receives the message and displays it. These programs are written for the Windows 95 or Windows NT command line. To see how they work, start two MS-DOS windows, start the server program first in one of the windows, and then start the client program in the other. The client sends its message to the server; the server, in turn, displays the message it receives and then terminates.

Listing 13.3. Intertask communication using shared memory: The server.

#include <iostream.h>
#include <windows.h>

void main(void)
{
HANDLE hmmf;
LPSTR lpMsg;
hmmf = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, 0x1000, "MMFDEMO");

if (hmmf == NULL)
{
cout << "Failed to allocated shared memory.\n";
exit(1);
}
lpMsg = (LPSTR)MapViewOfFile(hmmf, FILE_MAP_WRITE, 0, 0, 0);

if (lpMsg == NULL)
{
cout << "Failed to map shared memory.\n";
exit(1);
}
lpMsg[0] = '\0';
while (lpMsg[0] == '\0') Sleep(1000);
cout << "Message received: " << lpMsg << '\n';
UnmapViewOfFile(lpMsg);
}

Listing 13.4. Intertask communication using shared memory: The client.
#include <iostream.h>
#include <windows.h>

void main(void)
{
HANDLE hmmf;
LPSTR lpMsg;
hmmf = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
PAGE_READWRITE, 0, 0x1000, "MMFDEMO");

if (hmmf == NULL)
{
cout << "Failed to allocated shared memory.\n";
exit(1);
}
lpMsg = (LPSTR)MapViewOfFile(hmmf, FILE_MAP_WRITE, 0, 0, 0);

if (lpMsg == NULL)
{
cout << "Failed to map shared memory.\n";
exit(1);
}

strcpy(lpMsg, "This is my message.");
cout << "Message sent: " << lpMsg << '\n';
UnmapViewOfFile(lpMsg);
}

These two programs are nearly identical. They both start by creating a file mapping of the system paging file with the name MMFDEMO. After the mapping is successfully created, the server sets the first byte of the mapping to zero and enters a wait loop, checking once a second to see whether the first byte is nonzero. The client, in turn, deposits a message string at the same location and exits. When the server notices that the data is present, it prints the result and also exits.


Both programs can be compiled from the command line: cl mmfsrvr.cpp and cl mmfclnt.cpp.


************************************************************
You can find this book and many book on page http://kitap.selcuk.edu.tr (I think it is unavailable now, but will be operational in a few days).

Mustafa Demirhan
GeneralRe: Passing strings between running apps Pin
Mustafa Demirhan8-Sep-00 13:47
Mustafa Demirhan8-Sep-00 13:47 
GeneralIdeas for a Doc/View Variation... Pin
Chris Losinger7-Sep-00 11:16
professionalChris Losinger7-Sep-00 11:16 
GeneralRe: Ideas for a Doc/View Variation... Pin
arf7-Sep-00 14:07
arf7-Sep-00 14:07 
GeneralRe: Ideas for a Doc/View Variation... Pin
Sam Hobbs8-Sep-00 9:10
Sam Hobbs8-Sep-00 9:10 
GeneralProblem with radio buttons using win32 only.. Pin
Keegan Prendergast7-Sep-00 9:30
sussKeegan Prendergast7-Sep-00 9:30 
GeneralUnable to Debug MFC? (URGENT) Pin
a.r.f.7-Sep-00 6:11
a.r.f.7-Sep-00 6:11 
GeneralProblem Solved Re: Unable to Debug MFC? (URGENT) Pin
a.r.f.7-Sep-00 8:18
a.r.f.7-Sep-00 8:18 
Generalerror C2011 Pin
_ra7-Sep-00 5:16
_ra7-Sep-00 5:16 
GeneralRational Rose Experts Pin
Yaron7-Sep-00 2:25
Yaron7-Sep-00 2:25 
GeneralChooseFolderAPI Pin
Member 2003617-Sep-00 2:22
Member 2003617-Sep-00 2:22 
GeneralDisable a row in list control Pin
Eq6-Sep-00 18:05
Eq6-Sep-00 18:05 
GeneralRe: Disable a row in list control Pin
Michael Dunn6-Sep-00 21:40
sitebuilderMichael Dunn6-Sep-00 21:40 
GeneralRestricting access to DB Pin
Paul Ebert6-Sep-00 13:05
Paul Ebert6-Sep-00 13:05 
GeneralRe: Restricting access to DB Pin
Alex6-Sep-00 21:19
Alex6-Sep-00 21:19 
GeneralBOOL, TRUE, FALSE vs. bool, true, false Pin
Marty6-Sep-00 10:14
Marty6-Sep-00 10:14 
GeneralRe: BOOL, TRUE, FALSE vs. bool, true, false Pin
Michael Dunn6-Sep-00 16:06
sitebuilderMichael Dunn6-Sep-00 16:06 
GeneralRe: BOOL, TRUE, FALSE vs. bool, true, false Pin
Uwe Keim6-Sep-00 22:19
sitebuilderUwe Keim6-Sep-00 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.