No announcement yet.

sharing data between two EXEs

  • Filter
  • Time
  • Show
Clear All
new posts

  • sharing data between two EXEs

    Aloha again everyone! I'm almost afraid to post another question given
    the four-page debate that ensued after my last question!

    Anyway, I have a question that's probably surfaced before. I searched
    briefly in this forum, but couldn't find anything related. If someone has
    answered this before, sorry, and please post a link.

    Simply stated, I want a Quick-and-Dirty way of sharing some memory between
    two distinct EXE files, both created with PBCC4.

    Let's say A.EXE generates about 512 bytes of data. It creates new data
    on a non-deterministic timeframe, but usually about 0.5 to 2 Hz. In other
    words, every 0.5 to 2 seconds, it has 512 new bytes of data to share.

    B.EXE wants the data as soon as possible after it's created. B.EXE is also
    non-deterministic, but manages some other near-real-time tasks, so it
    cannot be burdened with waiting around for new data.

    Originally, I used a simple text file on the hard drive. A.EXE wrote new
    data when it was generated, and B.EXE read the same file whenever it had
    a chance to do so. The downside was constant drive activity and the necessity
    to manage read/write collisions (both EXEs opening the file at the same time).

    Then I switched to UDP. It was more elegant, but B.EXE still sometimes sits
    around waiting for data. A.EXE sends out data via UDP in-the-blind (it doesn't
    know if anyone is listening). But B.EXE has to stop and listen for any broadcasts.
    I set the UDP TIMEOUT value low (100 milliseconds), but B.EXE's overall
    performance still seems slow. In other words, B.EXE listens for a UDP broadcast
    for only 100 milliseconds. If it TIMEOUTS, it gives up completely and tries
    again the next time around.

    I'm tempted to go back to the text file solution.

    Is there a way two distinct EXEs can share 512 bytes of common memory?
    And just read and write on-demand? I've even considered converting the data
    into 24-bit chunks which can be used as color data for 171 pixels. A.EXE
    could plot the pixels and B.EXE could read them. Sounds crazy, but is there
    an easier way?

    Christopher Becker
    Advanced Airborne Test Facility
    Island of Kauai, Hawaii
    Christopher P. Becker
    signal engineer in the defense industry
    Abu Dhabi, United Arab Emirates

  • #2

    The keywords you need to search for on these forums are:
    "Inter Process Communication".

    If you were using PBWin compiler I would suggest to use the %WM_COPYDATA message.
    That way you can send data to another process' window message loop.
    But I don't think that can be done with the PBCC compiler.

    In that case, maybe memory-mapped files (MMF) could be a good solution .
    Plenty examples of those around.

    Kind regards

    Eddy -- HIME Huge Integer Math and Encryption library--


    • #3
      By coincidence this moring I discovered an unexpected form of Inter Process Communication.
      Two applications running in different CMD shells under XP managed to "communicate" by one setting the date and the other crashing because of it!



      • #4
        If one EXE is started as an child of the other you may pass the data useing the ENVIRON($) statment/function pair



        • #5
          win32: memory mapped files/control user count april 26, 2001
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]


          • #6
            ' Demo of Memory mapped file to share data between Exe's or Dll's
            ' If you start the program twice, the first one should set event and data,
            ' and the second one will react on Event.
            ' For sharing between more as 2 programs, the Event should be replaced by a
            ' Semaphore
            ' Sept, 16, 2003 - P.Lameijn
            #Include ""
            ' MMapStruct should hold all data to share between the processes...
            Type MMapStruct
              MDate     As String * 10
              MTime     As String * 10
              MyLong    As Long
              MyWord    As Word
              MyQuad    As Quad
            End Type
            Global MMapEvent As Long
            Function PBMain
              Dim hMap As Long                                          'Handle to map
              Dim pMap As MMAPSTRUCT Ptr                                'Pointer to map
              Dim szMapName As Asciiz * 256                             'Name of map
              Dim Mapped As Long                                        'First time mapped?
              Dim WFSO As Long                                          'WFSO result
              Dim MapSize As Long                                       'Size of map
            ' Set Name and Size of memory map
            ' The actual allocated size can be bigger due to memory allocation granularity
            ' (See: GetSystemInfo -> System_Info.dwAllocationGranularity)
              szMapName = "MyFileMap"
              MapSize   = SizeOf(MMAPSTRUCT)
            ' Create and Map fileMap. Sets pMap as pointer to MMAPSTRUCT
              hMap   = CreateFileMapping(ByVal -1, ByVal %Null, ByVal %PAGE_READWRITE, _
                       ByVal 0, MapSize,szMapName)
              If hMap Then
                If GetLastError = %ERROR_ALREADY_EXISTS Then Mapped = 1
                pMap = MapViewOfFile (hMap, ByVal %FILE_MAP_ALL_ACCESS, 0, 0, 0)
                If pMap = 0 Then Print "Mapping failed" : Exit Function
              End If
            ' Create Event
              MMapEvent = CreateEvent (ByVal 0, ByVal 0, ByVal 0, "MyEvent")
              Print "Press any key to end"
            ' Loop until keypress. If Mapped = 0, send data, else wait for event
              While Not InStat
                If Mapped = 0 Then
                  @pMap.MDate = Date$
                  @pMap.MTime = Time$
                  SetEvent MMapEvent
                  Print "Sending date -->> " + @pMap.MDate + ", time: " + @pMap.MTime
                  Sleep 1000
                  WFSO = WaitForSingleObject(ByVal MMapEvent, 2000)
                    Select Case WFSO
                      Case %WAIT_OBJECT_0
                        Print "Receiving date <<-- " + @pMap.MDate + ", time: " + @pMap.MTime
                  End Select
                End If
            ' Free map
              UnMapViewOfFile pMap
              CloseHandle hMap
            End Function

            [This message has been edited by Peter Lameijn (edited April 18, 2007).]


            • #7
              Does program B just sit there waiting for program A to create this data, then it does something and starts waiting again?

              If so, piping it via named pipe might make some sense.

              Or if you control the initial execution of program B, you could set it up as a child process and just feed it to B's STDIN by starting B with CreateProcess/handle overrides.

              Or, to use the Memory mapped file approach, you could have B wait on a named event, and A sets or pulses that event when it has data available, and B now knows it can go get it from the MMF. (this I think iffy if B can't keep up with A; the pipe is a much better way to go in this case).

              [This message has been edited by Michael Mattias (edited April 18, 2007).]
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]


              • #8
                If one program is slave to the other (nothing to do but wait for
                the first program to provide work), then you don't need to have the
                second program running all the time. You can cause it to execute
                when there is work for it to do, or you might consider a design
                where its all one program.

                Old Navy Chief, Systems Engineer, Systems Analyst, now semi-retired


                • #9
                  > or you might consider a design where its all one program.

                  e.g., anonymous pipe as job queue demo 10-29-03
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]


                  • #10

                    Here is my quick and dirty solution.

                    A.EXE open a random access file read and write shared and keep it
                    open all the time while adding 512 bytes records as needed.

                    B.EXE simply watch the LOF() of that file and read the new records
                    as they are added to the file.

                    You will not have any collision and the disk activity will be at a

                    A nice bonus will be a permanent record of your data made in real time.
                    That file could be restarted every day or week or month so as to keep
                    it at a handy size or simply flushed if not needed.

                    Old QB45 Programmer
                    Old QB45 Programmer


                    • #11
                      >Here is my quick and dirty solution....

                      Congratulations! You have just invented pipes.
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]


                      • #12
                        WM_SETTEXT and WM_GETTEXT can be used interprocess.
                        You might consider to create a control/window in a thread so any situation would not block APP A being busy collecting data.