Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Memory Mapped Files

Aug
1,941
71
Sometimes I want to share data between TCC and applications.

In this case, I use a memory mapped file.

The following opens a memory mapped file called mmf1;
Code:
E:\Utils>mmfopen.btm
mmf1=1136

Write a string to the memory mapped file;

Code:
E:\Utils>mmfwrite.btm Today is Monday.

Read a string from the memory mapped file;

Code:
E:\Utils>mmfread.btm
Today is Monday.

Save the contents of the memory mapped file to an environment variable;

Code:
E:\Utils>set mmfstuff=%{mmfread.btm}

E:\Utils>echo %mmfstuff
Today is Monday.

Close the memory mapped file.

Code:
E:\Utils>mmfclose.btm
mmf1=1352
mmfstuff=Today is Monday.
MMF1 is open
tcc.exe            pid: 2756   type: Section        548: \Sessions\1\BaseNamedObjects\MMF1
mmf1=0
mmfstuff=Today is Monday.

Notes:
mmfwrite.btm will also take input from a file with one line;
Code:
mmfwrite.btm < r:\test.txt
One can also pipe data into mmfwrite.btm;
Code:
echo %_time |! mmfwrite.btm

Single apostrophes need to wrap arguments containing a comma;
Code:
E:\Utils>mmfwrite `Monday, Monday`

E:\Utils>mmfread 
Monday, Monday

These are just skeleton mmf*.btm files,
as they can be expanded upon to accommodate multiple
memory mapped files.

These files work for me,
allowing me to exchange data between
64-bit TCC and 32-bit Visual Basic 6.0,
and 32-bit Visual FoxPro.

Hopefully others may find this useful, and expand on them.

Joe

Code:
@setlocal
@echo off
::mmfopen.btm
::
:: Determine if a handle to shared memory is open
::
iff defined mmf1 then
  set mmf*
  echo MMF1 is already open
  ::
  :: handle.exe is from https://learn.microsoft.com/en-us/sysinternals/downloads/handle
  ::
  handle.exe mmf1 -nobanner
  quit
endiff
set mmf1=%@smopen[10240,MMF1]
set mmf*
endlocal mmf1

Code:
@setlocal
@echo off
::mmfwrite.btm
iff defined mmf1 then
  :: echo mmf1 Exists
else
  echo mmf1 does not exist
  quit
endiff

:: 0 = Re-direct
::
:: mmfwrite.btm < r:\test.txt
:: echo %_time |! mmfwrite.btm
::
:: 1 = Console
::
:: mmfwrite.btm This is a test.

iff %# eq 0 then
  iff %_stdin eq 1 then
    echo USAGE: %_batchname Text to store in MMF
    quit
  endiff
endiff

Gosub ClearMMF
on error goto errTrap
iff %_stdin eq 1 then
  iff %@smwrite[%mmf1,0,a,%$] ne 0 then
    echo Error writing to MMF
  endiff
else
  do line in @con:
    iff %@smwrite[%mmf1,0,a,%line] ne 0 then
      echo Error writing to MMF
    endiff
  enddo
endiff
endlocal
quit

:errTrap
::  6 The handle is invalid.
iff %_syserr eq 6 then
  echo %@errtext[6]
else
  echo %@errtext[%_syserr]
endiff
quit
Return

:ClearMMF
set MMF1Len=%@len[%{mmfread.btm}]
set MMF1Len=%@dec[%MMF1Len]
do kount=0 to %MMF1Len
  echo %@smpoke[%mmf1,%kount,4,0] > nul
enddo
Return

Code:
@setlocal
@echo off
::mmfread.btm
on error goto errTrap
echo %@smread[%mmf1,0,a,10240]
endlocal
quit

:errTrap
echo %_syserr
:: 87 The parameter is incorrect.
quit
Return

Code:
@setlocal
@echo off
::mmfclose.btm
::
:: Determine if a handle to shared memory is open
::
iff defined mmf1 then
  set mmf*
  echo MMF1 is open
  ::
  :: handle.exe is from https://learn.microsoft.com/en-us/sysinternals/downloads/handle
  ::
  handle.exe mmf1 -nobanner
  set mmf1=%@smclose[%MMF1]
  set mmf*
  defer unset mmf1
endiff
endlocal
 
Back
Top