Welcome!

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

SignUp Now!

Using ALIAS as Shared Memory

Aug
1,941
71
There are many ways to share memory with TCC 64-bit and a 32-bit application.

Here's a method that I use.

In the source code below,
and the notes that follow,
I have a few issues,
but they have not caused any problems,
or crashes,
when using this method.

Code:
@echo off
iff %_shralias ne 1 then
  echo Shralias must be in memory.
  quit
endiff

iff %_admin ne 1 then
  echo Admin level is required to run %_batchname
  quit
endiff

unalias smjlcav*
memory | ffind /kvme"global|free"
test.exe
echo Contents of the smjlcav alias...
alias smjlcav
echo Storing the contents of smjlcav to an environment variable...
set smjlcav=%@alias[smjlcav]
echo smjlcav = : %smjlcav
unalias smjlcav*

I use 32-bit Visual Basic 6.0,
32-bit Visual FoxPro 9.0
and 32-bit PowerBASIC

It should be easy for C++ people to re-create this.

I chose PowerBASIC for this example,
as it is lower-level than VB6 or VFP9

PowerBASIC Code (compiled to test.exe)
Code:
#COMPILE EXE
#DIM ALL
#INCLUDE "Win32api.inc"

FUNCTION PBMAIN () AS LONG
  LOCAL hMap AS LONG, MapName AS ASCIIZ * 64, lRet AS LONG
  LOCAL kount AS LONG, AliasString AS STRING, mmsize AS LONG, theEof AS LONG

  '\Sessions\1\BaseNamedObjects\SharememUAliasjlcav
  MapName = "SharememUAliasjlcav" '<-- Change this to your MapName

  lRet = OpenFileMapping _
         (%FILE_MAP_ALL_ACCESS,_ ' access mode
         BYVAL %NULL, _ ' inherit flag
         MapName) ' pointer to name of file-mapping object

  IF lRet THEN
    hMap = MapViewOfFile _
           (lRet,_ ' file-mapping object to map into address space
           %FILE_MAP_ALL_ACCESS,_ ' access mode
           0,_ ' high-order 32 bits of file offset
           0,_ ' low-order 32 bits of file offset
           0) ' number of bytes to map

    ' The TCC Memory command returns...
    ' 262,144 characters total global alias
    ' 262144 * 2 = 524288
    ' Would like to determine mmfsize
    '  instead of hard-coding size.
    mmsize = 524288

    AliasString = ACODE$(PEEK$(hMap,mmsize))

    theEof = INSTR(AliasString,CHR$(0) + CHR$(0))
    STDOUT "Length of Shared Memory in use: ";
    STDOUT STR$(theEOF)
    ' In my case,
    '   this returns 22,481
    '
    ' TCC Memory command returns...
    ' 262,144 characters total global alias
    ' 239,662 characters free
    ' -------
    ' 22,482 <- 1 byte difference.

    FOR kount = 1 TO mmsize
      IF MID$(AliasString, Kount, 1) = CHR$(0) AND MID$(AliasString, Kount + 1, 1) = CHR$(0) THEN
        MID$(AliasString,kount) = CHR$(0) + "smjlcav=This is shared memory" + CHR$(0)
        POKE$ hMap,UCODE$(AliasString)
        EXIT FOR
      END IF
    NEXT kount
  ELSE
    STDOUT "Cannot open file mapping" + CHR$(10) + CHR$(13) + "Filemap must exist!"
  END IF
  UnmapViewOfFile BYVAL lRet
  CloseHandle hMap
END FUNCTION

Notes:
Running TEST.EXE repeatedly will create several SMJLCAV aliases!
Code:
E:\...\Alias>test.exe
Length of Shared Memory in use:  22481

E:\...\Alias>test.exe
Length of Shared Memory in use:  22511

E:\...\Alias>test.exe
Length of Shared Memory in use:  22541

E:\...\Alias>alias smjlc*
smjlcav=This is shared memory
smjlcav=This is shared memory
smjlcav=This is shared memory

To remove all of the SMJLCAV aliases...
Code:
E:\...\Alias>unalias smjlcav*

E:\...\Alias>alias smjlc*
TCC: Not an alias "smjlc*"

I have used Alias as Shared Memory.
It may also be possible to use Function and History as Shared Memory.

I used handle.exe from www.sysinternals.com to get the name of Alias MMF
You could also use Process Explorer from the same site.

Code:
tcc.exe pid: 6088 DESKTOP-LFBF7P3\jlcav
  37C: Section       \Sessions\1\BaseNamedObjects\SharememUAliasjlcav
  3E0: Section       \Sessions\1\BaseNamedObjects\SharememUFunctionjlcav
  3E4: Section       \Sessions\1\BaseNamedObjects\SharememUHistoryjlcav
  3E8: Section       \Sessions\1\BaseNamedObjects\SharememUDirHistoryjlcav

I have been using this method for several years,
and am posting it in the event others might find it useful.

Joe

Code:
     _x64: 1
   _admin: 1
_elevated: 1

TCC  31.01.22 x64   Windows 10 [Version 10.0.19045.4046]
BuildNumber  Caption                   CSDVersion  OSArchitecture  Version
19045        Microsoft Windows 10 Pro              64-bit          10.0.19045
 
Back
Top