Skip to main content
gamedev.net
🔒 Locked

Program Communication

Started by Running_Wolf Apr 14, 2005 at 1:37 PM 9 replies 1.3k views
Original Post
Running_Wolf
Running_Wolf
Hi, I was just wondering what the best way to share information between two simultaneously running programs is. Using a temporary, saved file on the hard disk seems a little crude. Are there any other ways? Thanks in advance.
L.I.G. == Life Is Good
mmelson
mmelson
There are a few ways. You should take a look at shared memory, pipes, and sockets.

Personally, whenever I have to share stuff between two programs that are running on the same machine, I tend to default to the shared memory approach.
Trap
Trap
The most generic method is probably using sockets.
mmelson
mmelson
Yeah, sockets do have the benefit of being easily extended to work between two machines, but I like being able to use the shared memory as I would any other memory.

But, as usual, the best solution depends on what the programs are doing.
nmi
nmi
Sockets are slow, your data has to go through the network stack.
Use shared memory instead.

Call CreateFileMapping with hFile set to (HANDLE)0xFFFFFFFF to create a shared memory region in the pagefile (also set dwMaximumSizeHigh and dwMaximumSizeLow in this case), or the handle to an open file to use this instead.

HANDLE hMapFile;hMapFile = CreateFileMapping(hFile,    // Current file handle.     NULL,                              // Default security.     PAGE_READWRITE,                    // Read/write permission.     0,                                 // Max. object size.     0,                                 // Size of hFile.     "MyFileMappingObject");            // Name of mapping object.  if (hMapFile == NULL) {     ErrorHandler("Could not create file-mapping object."); } 



Then call MapViewOfFile:

LPVOID lpMapAddress;lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.     FILE_MAP_ALL_ACCESS,               // Read/write permission     0,                                 // Max. object size.     0,                                 // Size of hFile.     0);                                // Map entire file.  if (lpMapAddress == NULL) {     ErrorHandler("Could not map view of file."); } 



For the second process, call OpenFileMapping:

HANDLE hMapFile;LPVOID lpMapAddress;hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.     FALSE,                             // Do not inherit the name    "MyFileMappingObject");            // of the mapping object.  if (hMapFile == NULL) {     ErrorHandler("Could not open file-mapping object."); }  lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.     FILE_MAP_ALL_ACCESS,               // Read/write permission.     0,                                 // Max. object size.     0,                                 // Size of hFile.     0);                                // Map entire file.  if (lpMapAddress == NULL) {     ErrorHandler("Could not map view of file."); }

Running_Wolf
Running_Wolf
Okay, thanks for the input thus far. One more thing, (and I am looking on MSDN for more info but meanwhile...), after setting up the shared memory object as the example gave. How do I set which variables and whatnot are shared?
L.I.G. == Life Is Good
Vanke
Vanke
Don't forget semaphores with you're shared memory or you could end up in lots of trouble. Or is that only a linux thing?
nmi
nmi
Once you called MapViewOfFile, you get a pointer to the memory mapped region of the shared file (the pagefile or a normal one).

Every access to this memory is visible in both processes. So you can just set a pointer to point into this region, and the object it points to will be shared:

struct MyData {  int i;};MyData* myPointer = (MyData*)lpMapAddress;myPointer->i = 3;


This will set i to 3 for the other process too.
nmi
nmi
Quote:
Original post by Vanke
Don't forget semaphores with you're shared memory or you could end up in lots of trouble. Or is that only a linux thing?


No, Windows has semaphores too ;-)

Call CreateSemaphore with lpName set to an uniqe string, so that both processes use the same semaphore.
Running_Wolf
Running_Wolf
Okay, thanks for all the input. I will start experimenting with it.
L.I.G. == Life Is Good
iMalc
iMalc
Don't overlook WM_COPY_DATA it is very useful, even if just to kick off the other method of communication mentioned.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.