News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Passing data between process.

Started by xanatose, November 07, 2016, 01:55:23 AM

Previous topic - Next topic

xanatose

I need to share data between two process. I usually use threads and DLL's but I have not much experience with processes.
Ideally I would like a way to access the memory of another process. But so far my attempts to do this have being futile.

I am not sure if this makes sense or not, but from what I gather over the internet. 

1. Create a named memory mapped file on process A
2. Open named memory mapped file on process B.
3. Create whatever structure inside the memory mapped file and operate from it.
4. Instead of using pointers use offsets with respective to the start of the mapped memory to share between process.
5. From B to A send, using a registered windows message. that the application want to receive data. This allows Application A to know that B exists.
6. Using a windows message, instead of sending a pointer send the offset of the pointer relative of the start of the mapped memory.
7. Use offsets relative to the start of the mapped file instead of pointers.
8. All offsets must be less than the size of the mapped file.
9. The mapped file is of static size???

Is this the process to do this?

Some other questions:

Can I simply map the whole file and treat it as a big array?

Is there a way to resize the mapped file dynamically or do I need to create a really huge file?  I do not want to waste memory, but I also do not want to worry about a program crashing because of not being able to add more data to the file.

Would setting the file time make sure that the other process updates its memory map or is this step unnecessary?

Would pipes be faster to send the messages than windows messages?

What gotcha's do I need to look for out when using memory mapped files?

How does one create a one writer multiple reader mutex for use between process? Is this needed or will the data be automatically locked while writing?

mabdelouahab


hutch--

xanatose,

A 32 bit app can read a memory mapped file created by a 64 bit app and I imagine vice versa but there are a couple of gotchas, they cannot be dynamically resized and the 32 bit app cannot read more than its normal memory range which is slightly under 2 gig. I have a test piece of 1 gig shared between the two processes but the 32 bit version crashed on 2 gig. If you can establish a communication between a 32 and 64 bit app with messaging, if you need to resize a memory mapped file, you must destroy it and start again and this involves destroying it in all processes that have called it as it will not close until all instances calling it have exited the mmf.

hutch--

I am not sure what you are working in, 32 or 64 bit but the code to communicate between 2 apps is much the same.

handles

      hMMF dd ?  ; memory mapped file handle
      pMMF dd ?  ; pointer to the mapped memory
      WM_PRIVATEMSG1 dd ?  ; define a private message
      WM_PRIVATEMSG2 dd ?  ; define a private message


Create the private messages

    mov WM_PRIVATEMSG1, rv(RegisterWindowMessage,"private_msg_1")
    mov WM_PRIVATEMSG2, rv(RegisterWindowMessage,"private_msg_2")


Process the private messages in your WndProc like normal

      Case WM_PRIVATEMSG1
        fn SendMessage,HWND_BROADCAST,WM_PRIVATEMSG2,0,0  ; send another private message back to the original sender


Works fine, its clean, fast, simple and it works between 32 and 64 bit executables.

Note that you cannot use memory addresses between separate processes as the are not in the same memory space. The address provided by the memory mapped file is the only address you can use but you can chop it up any way you like between processes. The private messages give you control of what you need as far as actions go.


mabdelouahab

hutch--
This is just a simple example (CONSOLE), Not a perfect example but illustrate that you can use  memory mapped file without using Window Message,
It only needs to arrange the house the way that serve a particular idea.
run the program several times at the same time, enter the name then select which process you want to send a message.

xanatose

Quote from: hutch-- on November 07, 2016, 09:22:59 PM
I am not sure what you are working in, 32 or 64 bit but the code to communicate between 2 apps is much the same.

Nothing to worry about. Just the standard plans to take over the world.  :badgrin:

I got one table of data that changes rapidly (sometimes less than 1ms).  I need other process to access this table wherever they can, the process writing cannot stop, but the other process can. All in the same machine. So thought that instead of copy the data on each process to a message, I pass the index of the item that has changed. Then, using the memory mapped file, the other process can access the data on a read only way.

If done on a DLL I would have just passed a pointer. But since they are different process. I could not find a way to share the memory except by memory mapped files. Unless of course I am missing another api that let me allocate memory that could be used on more than  one process.

Anyway. Got it to work on a somehow cumbersome way. But found some malloc/free routines in the "C programming language" book that will translate to assembler and to use offsets instead of pointers.  That way I could use the routines in an easier way.

Will pass the routines to assembler. and then share them in this forum as they might helps someone else. Well that is the innocent excuse, in reality part of my evil plan is to find unsuspecting victims to read the code and point any bug I missed (starts evil laugh)  : :badgrin:.