  'GuiInterface.inc
  '20/05/2023
  '15/05/2025
  'CP
  '
  '  This file can be included in both client and
  '  server source code
  '
  '  USAGE:
  '  ======
  '
  '  SERVER ONLY:
  '  % GuiStateServer
  '  % GuiSharedMem "Local\WinGuiState" 'USING NAMED SHARED MEMORY
  '  uses GuiInterface
  '
  '  CLIENT ONLY:
  '  % GuiDllName "MyGuiLib.dll" 'your server DLL
  '  % GuiSharedMem "Local\WinGuiState"
  '  uses GuiInterface
  '
  '
  sys GuiMemHandle
  sys GuiMemBuffer
  '
  #ifdef GuiStateServer
    '
    #ifdef GuiSharedMem
      sys*bu
      ' PAGE_READWRITE 0x04
      ' case sensitive name
      sys h=-1 '64bit pseudo handle    
      GuiMemHandle = CreateFileMapping(h,0,0x4,0,0x1000,GuiSharedMem)
      if GuiMemHandle == 0
        ers="Could not create file mapping object"
        ert=437
      else
        '0x7 read/write permission
        @bu = MapViewOfFile( GuiMemHandle, 0x7, 0, 0, 0x1000)          
        if @bu == 0
          ers="Could not map view of file" + cr + 
          ert=438 
        endif
      endif
      GuiMemBuffer=@bu
    #else 'NO SHARED MEM
      sys bu[0x1000] 'STATIC BUFFER TO HOLD SHARED STATE VARIABLES
    #endif
    '
    function guistate() as sys export 
      return @bu
    end function
    undef bu 'end bu scope
    '
  #else 'CLIENT
    '
    #ifdef GuiSharedMem
      sys*bu
      ' FILE_MAP_ALL_ACCESS 0x1f
      ' Case Sensitive Name
      GuiMemHandle = OpenFileMapping(0x1f,0,GuiSharedMem)
      if h == 0
        ers="Could not open file mapping object"
        ert=439
      else
        '0x7 read/write permission
        @bu = MapViewOfFile( GuiMemHandle, 0x7, 0, 0, 0x1000)          
        if @bu == 0
          ers="Could not map view of file" + cr + 
          ert=440 
        endif
      endif
      function GuiState() as sys
        return @bu
      end function
      GuiMemBuffer=@bu
      undef bu 'end bu scope
    #else 'NO SHARED MEMORY, USING DLL CALL INSTEAD
      declare guistate lib GuiDllName () as sys
    #endif
    '
  #endif 'SERVER / CLIENT

  sys b = guistate()
  if ert
    print ers cr ert
  endif
 
  '
  bind b 
    sys     hWndMain,hInst,inst,hDC,hRC
    int     pixelform
    int     mposx,mposy
    int     sposx,sposy
    int     eposx,eposy
    int     iposx,iposy
    int     mmove,bleft,bmid,bright,bwheel
    int     pause
    int     bkey,keyd,lastkey,lastchar
    int     running
    int     key[256]
  end bind

  #undef b 'end b scope

  
