Author Topic: Who wants duplicate my experience with COMMON SHARED?  (Read 3688 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Who wants duplicate my experience with COMMON SHARED?
« on: June 27, 2021, 05:18:49 pm »
Hi QB64 community
Hi QB64 friends

I have tried to build up a little program that runs another that runs another... and all these little programs have a COMMON SHARED area to exchange  some values....

Well, nothing to do is my result!

To get the same experience
1. copy these 3 codes, compile without running (F11)
2. open the folder of .EXE and run the first file...
3. don't hurt yourself! Quickly close all those windows that are popping up!

file1: Save this file as  multipleinstances 1.BAS
Code: QB64: [Select]
  1. Print " Hi ", iOne
  2.  
  3. If iOne = 0 Then
  4.     iOne = 1
  5.     _Title " First"
  6. Print "Now ione = "; iOne
  7.  
  8. If iOne < 2 Then Shell _DontWait "multipleinstances 2.exe" Else Print "Goodbye"
  9.  

file2: Save this file as multipleinstances 2.BAS
Code: QB64: [Select]
  1. Print " Hi ", iOne
  2.  
  3. If iOne = 1 Then
  4.     iOne = iOne + 1
  5.     _Title "Second "
  6.  
  7. Print "Now iOne = "; iOne
  8. If iOne < 2 Then Shell _DontWait "multipleinstances 3.exe" Else Print "Goodbye"

file3:Save  this file as multipleinstances 3.BAS
Code: QB64: [Select]
  1. Print " Hi ", iOne
  2.  
  3. If iOne = 2 Then
  4.     iOne = iOne + 1
  5.     _Title " Third"
  6. Print "Now iOne is "; iOne
  7.  
  8. If iOne < 2 Then Shell _DontWait "multipleinstances 3.exe" Else Print "Goodbye"
  9.  

Waiting your suggestions. Thanks
Programming isn't difficult, only it's  consuming time and coffee

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #1 on: June 27, 2021, 08:31:45 pm »
I have an old program that was written in QB45 that uses COMMON SHARED and works fine.

(I would post the code, but it's just under 3,000 lines).

Here is the code I use with great success:
Code: QB64: [Select]
  1. '*****************************************
  2. '   DEFINE GLOBAL VARIABLES
  3. '*****************************************
  4. COMMON SHARED Number, RedBlack, OddEven, HighLow, Column, Dozen, BankRoll
  5. COMMON SHARED Bets, Table, PayoffAmount, BetAmount, BetType$, LineOut$
  6. COMMON SHARED NumberOfBets, BetNumber%, GoodBet, NumOut$, TypOut$
  7. COMMON SHARED windowTop!, windowBottom!, windowLeft!, windowRight!, FColor%, BColor%, WLBL$
  8. COMMON SHARED errWTop!, errWBottom!, errWLeft!, errWRight!, errWFClr%, errWBClr%
  9. COMMON SHARED errWLBL$, errTClr%, NOERRORS, GreenBack%, BlackBack%, RedBack%
  10. COMMON SHARED RowPointer%, ForeGroundColor%, BackGroundColor%
  11. COMMON SHARED Amount$, FileName$, SplitOut$, HeadingColor%
  12. COMMON SHARED IntroFColor1%, IntroFColor2%, IntroFColor3%, IntroFColor4%, IntroFColor5%
  13. COMMON SHARED IntroBColor1$, IntroBColor2%
  14. COMMON SHARED TableMinimum, TableMaximum, TotalInsideBets, TotalOutsideBets
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #2 on: June 27, 2021, 08:38:38 pm »
Hi QB64 community
Hi QB64 friends

I have tried to build up a little program that runs another that runs another... and all these little programs have a COMMON SHARED area to exchange  some values....

Well, nothing to do is my result!

To get the same experience
1. copy these 3 codes, compile without running (F11)
2. open the folder of .EXE and run the first file...
3. don't hurt yourself! Quickly close all those windows that are popping up!


COMMON SHARED was used to keep variables across MODULEs. As QB64 does not support MODULEs anymore its not going to work how your thinking it should, if you need to keep variable value between programs you will have to PUT and GET them to and  from a file in between programs
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #3 on: June 27, 2021, 09:05:08 pm »
Common Shared will work with arrays and variables in Include files.

Marked as best answer by TempodiBasic on June 29, 2021, 12:09:55 pm

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #4 on: June 28, 2021, 01:50:20 am »
Just from looking at the code (didn't test it), I would say the problem is that Tempodi is SHELLing out the other instances, which executes every instance in its own context.

As far as I know COMMON SHARED does only pass the variables when CHAINing from one to the other module, or maybe RUNing too, but definitly not SHELLing.

EDIT:
Nope, as per the Wiki, only CHAIN will pass COMMON SHARED variables.
« Last Edit: June 28, 2021, 03:28:39 am by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #5 on: June 29, 2021, 04:09:32 pm »
Hi QB64 community!
I love you!

Thanks for useful feedbacks!
thanks to:
@bplus  and @George McGinn  your informations have been useful to fix the search in my bad code.
@Cobalt  your workaround is good as true as SSD are faster then HDD of the past!
@RhoSigma  you have lighted me why my code is wrong, Yes I use SHELL _DONTWAIT and not CHAIN...

nevertheless I got my code running, but I got not 3 windows but one DOS window in which the output of the 3 different programs comes out!
So CHAIN is a good DOS emulator command and each module has been taken off of memory in the moment that the new module has been loaded into memory! .... No TSR program!

Programming isn't difficult, only it's  consuming time and coffee

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #6 on: June 29, 2021, 04:31:47 pm »
@TempodiBasic

Don't forget; you can also use my new shared memory library in Windows!

Parent:
Code: QB64: [Select]
  1.  
  2. Dim As _MEM pShare 'You need to use a _MEM block
  3. Dim As Long longtoShare: longtoShare = 500 'declaring a long variable to store in the _MEM block
  4. pShare = _Mem(longtoShare) 'storing the long variable in the _MEM block
  5.  
  6. Print CommonShared("MySharedMem", pShare) 'initializing the shared memory object
  7. _MemFree pShare
  8.  
  9. '$INCLUDE:'Win32 SharedMem.bas'

Child:
Code: QB64: [Select]
  1.  
  2. Dim As _MEM newMem: newMem = _MemNew(4) 'declaring the destination _MEM block
  3.  
  4. Print ReadShared("MySharedMem", newMem) 'reading the shared memory object into the new _MEM block
  5.  
  6. Dim As Long dest 'declaring a destination long variable
  7. _MemGet newMem, newMem.OFFSET, dest 'storing the contents of the _MEM block into a new long variable
  8. Print dest
  9. _MemFree newMem
  10.  
  11. '$INCLUDE:'Win32 SharedMem.bas'

Full $INCLUDE, save as Win32 SharedMem.bas
Code: QB64: [Select]
  1.     Function CreateFileMapping%& Alias "CreateFileMappingA" (ByVal hFile As _Offset, Byval lpFileMappingAttributes As _Offset, Byval flProtect As Long, Byval dwMaximumSizeHigh As Long, Byval dwMaximumSizeLow As Long, lpName As String)
  2.     Function OpenFileMapping%& Alias "OpenFileMappingA" (ByVal dwDesiredAccess As Long, Byval bInheritHabndle As Long, lpName As String)
  3.     Function MapViewOfFile%& (ByVal hFileMappingObject As _Offset, Byval dwDesiredAccess As Long, Byval dwFileOffsetHigh As Long, Byval dwFileOffsetLow As Long, Byval dwNumberOfBytesToMap As _Offset)
  4.     Sub UnmapViewOfFile (ByVal lpBaseAddress As _Offset)
  5.     Function GetLastError& ()
  6.     Sub CloseHandle (ByVal hObject As _Offset)
  7.  
  8. Function CommonShared%% (sharedName As String, pShared As _MEM)
  9.     Const INVALID_HANDLE_VALUE = -1
  10.     Const PAGE_READWRITE = &H04
  11.     Const FILE_MAP_ALL_ACCESS = &HF001F
  12.     Declare Dynamic Library "Kernel32"
  13.         Sub CopyMemory Alias "RtlCopyMemory" (ByVal Destination As _Offset, Byval Source As _Offset, Byval Length As _Offset)
  14.     End Declare
  15.     Dim As _Offset hMapFile, pBuf
  16.     Dim As Long sharedSize: sharedSize = Val(Str$(pShared.SIZE))
  17.     hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, sharedSize, sharedName)
  18.     If hMapFile = 0 Then
  19.         CommonShared = 0
  20.         Exit Function
  21.     End If
  22.     pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sharedSize)
  23.     If pBuf = 0 Then
  24.         CloseHandle hMapFile
  25.         CommonShared = 0
  26.         Exit Function
  27.     End If
  28.     CopyMemory pBuf, pShared.OFFSET, sharedSize
  29.     CommonShared = -1
  30.     EndShare hMapFile, pBuf
  31.  
  32. Function ReadShared%% (sharedName As String, destVar As _MEM)
  33.     Const FILE_MAP_ALL_ACCESS = &HF001F
  34.     Dim As _Offset hMapFile, pBuf
  35.     Dim As Long sharedSize: sharedSize = Val(Str$(destVar.SIZE))
  36.     hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, sharedName)
  37.     If hMapFile = 0 Then
  38.         ReadShared = 0
  39.         Exit Function
  40.     End If
  41.     pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sharedSize)
  42.     If pBuf = 0 Then
  43.         ReadShared = 0
  44.         Exit Function
  45.     End If
  46.     destVar = _Mem(pBuf, sharedSize)
  47.     ReadShared = -1
  48.     EndShare hMapFile, pBuf
  49.  
  50. Sub EndShare (hMap As _Offset, pMapBuf As _Offset)
  51.     UnmapViewOfFile hMap
  52.     CloseHandle pMapBuf
Shuwatch!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #7 on: June 30, 2021, 04:43:01 am »
@SpriggsySpriggs
Interesting workaround that can make working sharing variable by RAM between 2 SHELL programs

Sure it must manage manually the exchanges between 2 programs but it CAN DO IT!
I'll try ,as I get some time to spend for project, to build 2 different programs that talks between them.

Thanks
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Who wants duplicate my experience with COMMON SHARED?
« Reply #8 on: July 04, 2021, 09:53:20 am »
Here a first step using the workaroung of Spriggsy
Parent version (programA) in which pressing Enter you decide the new long value to pass to child (programB)
Code: QB64: [Select]
  1. Const space = 32, enter = 13
  2. Dim K$, NamVar$
  3. Dim As _MEM pShare 'You need to use a _MEM block
  4. Dim As Long longtoShare, CountLong: longtoShare = 500 'declaring a long variable to store in the _MEM block
  5. pShare = _Mem(longtoShare) 'storing the long variable in the _MEM block
  6. Print CommonShared("MySharedMem", pShare) 'initializing the shared memory object
  7.  
  8. 'my mod
  9. Locate 1, 1: Print "Press space to quit program"
  10.     K$ = InKey$
  11.     If K$ = Chr$(space) Then Exit While
  12.     If K$ = Chr$(enter) Then
  13.         Input "please enter new value:", longtoShare
  14.         CountLong = CountLong + 1
  15.         pShare = _Mem(longtoShare) 'storing the long variable in the _MEM block
  16.         NamVar$ = Str$(CountLong)
  17.         Print CommonShared(NamVar$, pShare), longtoShare 'initializing the shared memory object
  18.     End If
  19. _MemFree pShare
  20.  
  21. '$INCLUDE:'Win32 SharedMem.bas'
  22.  

Child version (programB) in which you red the new value passed by Parent (programA)
Code: QB64: [Select]
  1. Const space = 32, enter = 13
  2.  
  3. Dim K$, NamVar$
  4. ReDim As _MEM newMem: newMem = _MemNew(4) 'declaring the destination _MEM block
  5.  
  6. Print ReadShared("MySharedMem", newMem) 'reading the shared memory object into the new _MEM block
  7. Dim As Long dest, countRead 'declaring a destination long variable
  8. _MemGet newMem, newMem.OFFSET, dest 'storing the contents of the _MEM block into a new long variable
  9. Print dest
  10.  
  11.  
  12.     K$ = InKey$
  13.     If K$ = Chr$(space) Then Exit While
  14.     If K$ = Chr$(enter) Then
  15.         newMem = _MemNew(4)
  16.         countRead = countRead + 1
  17.         NamVar$ = Str$(countRead)
  18.         Print ReadShared(NamVar$, newMem) 'reading the shared memory object into the new _MEM block
  19.         _MemGet newMem, newMem.OFFSET, dest 'storing the contents of the _MEM block into a new long variable
  20.         Print dest
  21.     End If
  22. _MemFree newMem
  23.  
  24. '$INCLUDE:'Win32 SharedMem.bas'
  25.  
The 2 kind of operations can be used for talking between 2 programs stay resident like in this example using SHELL _DONTWAIT.

Waiting feedbacks, Thanks to read and try
Programming isn't difficult, only it's  consuming time and coffee