Background
People are often scrounging for easy to use IPC alternatives for Visual Basic programs. Windows offers a ton of choices, but unless you can state your requirements it is difficult for somebody else to make a recommendation.
One obvious route is to use a common disk file shared among your program instances (processes). This really isn't as bad as it sounds, but there is a somewhat lighter-weight alternative to a shared temporary file that often isn't considered.
Files can be mapped into shared memory, but we can go a step further and "back" such shared memory with the system paging file instead. This thread talks about some simple code to do this in a bare bones fashion.
MMapper
MMapper is a simple class using the basic set of API calls required to create, open, read, write, and close such a chunk of shared memory.
As written, it sets up a BLOB of bytes as shared and then treats it as a single place to write or read data that multiple processes can use. Since you do not get fine control over where this memory is mapped into your processes' address spaces it is normally used along with CopyMemory() calls.
MMapper handles this by accepting pointer and length arguments for its read and write methods. This means you can pass String variables, Byte arrays, or even UDTs that are flat (do not contain dynamic-length fields).
The Demo
The attached demo contains the MMapper class and two projects: Writer and Reader.
Writer is responsible for creating the mapped file and writing data to it. Reader opens this mapped file (which requires that it is first created by Writer) and reads data from it.
MMapper creates/opens the mapped file with read/write access, so the fixed roles of Reader and Writer are simply to make the operation easier to understand.
You could easily have every program (and every instance of each program) try to create the mapped file, and they could all both read and write. It's just a matter of figuring out how you want to coordinate things among all of the running processes, and that part is up to you.
Here I use a small UDT to pass one Long value and one String * 100 value.
Enhancements
It is easy enough to extend MMapper in several ways.
Some of these involve security options to allow sharing among users. You can also treat the mapped space as several separate items instead of one BLOB. And you can map an actual disk file, providing persistence across sessions.
See your MSDN Library CDs or Windows SDK for more details.
This even works on Win9x with a few limitations inherent in the platform (such as no Terminal Services or Fast User Switching or other multi-user support).
People are often scrounging for easy to use IPC alternatives for Visual Basic programs. Windows offers a ton of choices, but unless you can state your requirements it is difficult for somebody else to make a recommendation.
One obvious route is to use a common disk file shared among your program instances (processes). This really isn't as bad as it sounds, but there is a somewhat lighter-weight alternative to a shared temporary file that often isn't considered.
Files can be mapped into shared memory, but we can go a step further and "back" such shared memory with the system paging file instead. This thread talks about some simple code to do this in a bare bones fashion.
MMapper
MMapper is a simple class using the basic set of API calls required to create, open, read, write, and close such a chunk of shared memory.
As written, it sets up a BLOB of bytes as shared and then treats it as a single place to write or read data that multiple processes can use. Since you do not get fine control over where this memory is mapped into your processes' address spaces it is normally used along with CopyMemory() calls.
MMapper handles this by accepting pointer and length arguments for its read and write methods. This means you can pass String variables, Byte arrays, or even UDTs that are flat (do not contain dynamic-length fields).
The Demo
The attached demo contains the MMapper class and two projects: Writer and Reader.
Writer is responsible for creating the mapped file and writing data to it. Reader opens this mapped file (which requires that it is first created by Writer) and reads data from it.
MMapper creates/opens the mapped file with read/write access, so the fixed roles of Reader and Writer are simply to make the operation easier to understand.
You could easily have every program (and every instance of each program) try to create the mapped file, and they could all both read and write. It's just a matter of figuring out how you want to coordinate things among all of the running processes, and that part is up to you.
Here I use a small UDT to pass one Long value and one String * 100 value.
Enhancements
It is easy enough to extend MMapper in several ways.
Some of these involve security options to allow sharing among users. You can also treat the mapped space as several separate items instead of one BLOB. And you can map an actual disk file, providing persistence across sessions.
See your MSDN Library CDs or Windows SDK for more details.
This even works on Win9x with a few limitations inherent in the platform (such as no Terminal Services or Fast User Switching or other multi-user support).