🔒 Locked
Program Communication
Advertisement
Original Post
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.
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.
Then call MapViewOfFile:
For the second process, call OpenFileMapping:
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."); }
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?
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:
This will set i to 3 for the other process too.
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.
Okay, thanks for all the input. I will start experimenting with it.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement