|
Post by thraka on Nov 7, 2007 19:06:25 GMT -5
I'm not sure I follow how the mem map file works. I have two processes and I really need them to communicate with eachother (on same machine) so I've done some researching and mem map file seems to be the best way to go. And behold vbCorLib supports it Do you have a working example of how to use this to enable two apps to communicate back and forth?
|
|
|
Post by Kelly Ethridge on Nov 7, 2007 23:37:09 GMT -5
Hi there,
That's an interesting idea you have there. I've whipped up a simple example showing an app that can be run multiple times and shares the same file that each app maps to. You can then write from one app and read from another.
The following code requires a textbox (txtWrite), a labal (lblRead), and two buttons (cmdRead, cmdWrite).
Private mFile As MemoryMappedFile Private mBytes() As Byte Private mStream As MemoryStream Private mWriter As StreamWriter Private mReader As StreamReader
Private Sub cmdRead_Click() mStream.Position = 0 lblRead.Caption = mReader.ReadLine End Sub
Private Sub cmdWrite_Click() mStream.Position = 0 Call mWriter.WriteLine(txtWrite.Text) End Sub
Private Sub Form_Load() If Not File.Exists("Shared.bin") Then ' build a 1K shared buffer Dim bytes() As Byte ReDim bytes(1023) Call File.WriteAllbytes("Shared.bin", bytes) End If Set mFile = Cor.NewMemoryMappedFile("Shared.bin", ReadWriteAccess, ReadWriteShare) mBytes = mFile.CreateView() Set mStream = Cor.NewMemoryStream(mBytes, 0, cArray.GetLength(mBytes)) Set mWriter = Cor.NewStreamWriter(mStream) mWriter.AutoFlush = True Set mReader = Cor.NewStreamReader(mStream) End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Call mFile.DeleteView(mBytes) Call mStream.CloseStream End Sub
I wasn't sure how or what type of information you wanted to pass around, so I just used a stream reader and writer for demonstration purposes.
So basically the MemoryMappedFile class provides access to the mapped file by creating a view of the file using a byte array. You could also just get the the base address of the mapped file from the BaseAddress property if you want to do direct memory manipulation instead of using the byte array.
I only created support for simple file mapping. If there are more advanced features that mapping can accomplish you may have to do more low-level calls.
I hope this gives you some idea of what I was thinking when I created the MemoryMappedFile class.
Take Care! Kelly
|
|
|
Post by thraka on Nov 12, 2007 16:02:11 GMT -5
Hmm Perhaps i'm using the wrong technology then.. Maybe a win sock socket between a child app and main app would be better suited (both running local, no remote) But I'd be unsure how it would be handled in vista etc... Or if I could use Named pipes create two on each process specifically for the communication that may work too....
But I need to do stuff like the main engine process starts up, loads a bunch of data runs a bunch of stuff. Then it spawns the child process and it needs to send it a set of instructions on what the engine wishes the child to do. The child does that. During that though, the child may ask the engine to run somet things and give it the results (strings or integers). So I need this two way connection open and flowing during the life cycle of the child process. and then eventually the child will close down but before doing so, give the engine a final list of results.
Remember though this isn't remote, it's all local. Any ideas? I was thinking of an activex exe or something. If i made it single apartement (i havne't used those terms in awhile i may be wrong in them hehe) I could have it running and both the engine and child initiate a CreateObject on it for talking. Kind of like what Excel and Word do. They are activex exe's and you can have multipe apps doing a createobject on them and talk to the current running instance..
|
|
|
Post by Kelly Ethridge on Nov 13, 2007 20:45:21 GMT -5
yes, the ActiveX EXE would probably be your best bet. Creating a server for your apps to communicate through seems like it should work just fine.
Memory mapped files are more for dealing with large files without having to read the entire thing into memory, but you access it as if it were all in memory. That is where the Byte array comes from for the MemoryMappedFile.CreateView method. It provides a Byte array mapped to the file and allows for easy access using standard Byte array manipulation.
|
|