home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples5 / ch14 / frmserv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.5 KB  |  168 lines

  1. VERSION 5.00
  2. Begin VB.Form frmServer 
  3.    Caption         =   "CheckStand"
  4.    ClientHeight    =   2865
  5.    ClientLeft      =   5475
  6.    ClientTop       =   1770
  7.    ClientWidth     =   4200
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   2865
  11.    ScaleWidth      =   4200
  12.    Begin VB.ListBox lstCustomers 
  13.       Height          =   1395
  14.       ItemData        =   "FRMSERV.frx":0000
  15.       Left            =   300
  16.       List            =   "FRMSERV.frx":0007
  17.       TabIndex        =   3
  18.       Top             =   1200
  19.       Width           =   3495
  20.    End
  21.    Begin VB.Timer Timer1 
  22.       Enabled         =   0   'False
  23.       Interval        =   400
  24.       Left            =   -120
  25.       Top             =   780
  26.    End
  27.    Begin VB.CheckBox chkOpen 
  28.       Caption         =   "Open"
  29.       Height          =   255
  30.       Left            =   2460
  31.       TabIndex        =   2
  32.       Top             =   240
  33.       Width           =   915
  34.    End
  35.    Begin VB.TextBox txtCheck 
  36.       Height          =   315
  37.       Left            =   1380
  38.       TabIndex        =   0
  39.       Text            =   "1"
  40.       Top             =   180
  41.       Width           =   795
  42.    End
  43.    Begin VB.Label lblStatus 
  44.       Height          =   195
  45.       Left            =   360
  46.       TabIndex        =   4
  47.       Top             =   780
  48.       Width           =   3495
  49.    End
  50.    Begin VB.Label Label1 
  51.       Alignment       =   1  'Right Justify
  52.       Caption         =   "Checkstand:"
  53.       Height          =   255
  54.       Left            =   180
  55.       TabIndex        =   1
  56.       Top             =   240
  57.       Width           =   1095
  58.    End
  59. Attribute VB_Name = "frmServer"
  60. Attribute VB_GlobalNameSpace = False
  61. Attribute VB_Creatable = False
  62. Attribute VB_PredeclaredId = True
  63. Attribute VB_Exposed = False
  64. Option Explicit
  65. ' Copyright 
  66.  1997 by Desaware Inc. All Rights Reserved
  67. Dim FileHandle As Long      ' Handle of open file
  68. Dim MappingHandle As Long   ' Handle to file mapping
  69. Dim MappingAddress As Long  ' Address of file mapping
  70. Dim Security As SECURITY_ATTRIBUTES
  71. Dim MutexHandle As Long     ' Handle of mutex for this server
  72. Private Sub chkOpen_Click()
  73.     Dim usename$
  74.     Dim InitialStand As CheckStand
  75.     Dim written&
  76.     ' Note lack of error checking here
  77.     usename$ = "ChkStd" & txtCheck.Text
  78.     If chkOpen.value = 1 Then
  79.         If FileHandle <> 0 Then Exit Sub
  80.         ' Create new file, read write access, exclusive use,
  81.         FileHandle = CreateFile(usename$, GENERIC_READ Or GENERIC_WRITE, _
  82.                                 0, Security, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_RANDOM_ACCESS, 0)
  83.         If FileHandle = -1 Then
  84.             MsgBox "Can't create file"
  85.             FileHandle = 0
  86.             Exit Sub
  87.         End If
  88.         ' Write the initial file info
  89.         Call WriteFile(FileHandle, InitialStand, Len(InitialStand), written, 0)
  90.         Call FlushFileBuffers(FileHandle)
  91.         ' Now we need a mapping
  92.         MappingHandle = CreateFileMapping(FileHandle, Security, PAGE_READWRITE, 0, 0, usename$ & "map")
  93.         If MappingHandle = 0 Then
  94.             MsgBox "Can't create file mapping"
  95.             Exit Sub
  96.         End If
  97.         MappingAddress = MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0)
  98.         If MappingAddress = 0 Then
  99.             MsgBox "Can't map view of the file"
  100.             Exit Sub
  101.         End If
  102.         ' Don't get initial ownership
  103.         MutexHandle = CreateMutexBynum(0, False, usename$ & "mutex")
  104.         Timer1.Enabled = True   ' Start watching for customers
  105.     Else
  106.         CleanUp
  107.         lblStatus.Caption = "Closed"
  108.     End If
  109. End Sub
  110. Private Sub Form_Load()
  111.     With Security
  112.         .nLength = Len(Security)
  113.         .lpSecurityDescriptor = 0
  114.         .bInheritHandle = True   ' Doesn't really matter
  115.     End With
  116. End Sub
  117. Private Sub Form_Unload(Cancel As Integer)
  118.     ' Remember, this won't get called if you Stop without
  119.     ' closing the main window.
  120.     CleanUp
  121. End Sub
  122. Private Sub CleanUp()
  123.     ' Remember, this won't get called if you Stop without
  124.     ' closing the main window.
  125.     Timer1.Enabled = False
  126.     If MappingAddress <> 0 Then
  127.         Call UnmapViewOfFile(MappingAddress)
  128.         MappingAddress = 0
  129.     End If
  130.     If MappingHandle <> 0 Then
  131.         Call CloseHandle(MappingHandle)
  132.         MappingHandle = 0
  133.     End If
  134.     If FileHandle <> 0 Then
  135.         Call CloseHandle(FileHandle)
  136.         FileHandle = 0
  137.     End If
  138.     If MutexHandle <> 0 Then
  139.         Call CloseHandle(MutexHandle)
  140.         MutexHandle = 0
  141.     End If
  142. End Sub
  143. Private Sub Timer1_Timer()
  144.     Dim cs As CheckStand
  145.     Static item%
  146.     Static tot As Single
  147.     ' Look at the checkstand
  148.     agCopyData ByVal MappingAddress, cs, Len(cs)
  149.     If cs.Done Then
  150.         If cs.Total <> 0 Then Exit Sub ' Waiting for payment
  151.         If item = 0 Then lblStatus.Caption = "Checking out " & cs.Client
  152.         tot = tot + cs.Prices(item)
  153.         item = item + 1
  154.         ' Stop at free item anyway
  155.         If item > UBound(cs.Prices) Or cs.Prices(item - 1) = 0 Then ' Done
  156.             lstCustomers.AddItem cs.Client & " " & Format$(tot, "0.00")
  157.             cs.Total = tot
  158.             cs.Done = 0
  159.             item = 0
  160.             tot = 0
  161.             lblStatus.Caption = "Waiting to pay"
  162.         End If
  163.         agCopyData cs, ByVal MappingAddress, Len(cs)
  164.     Else
  165.         lblStatus.Caption = "Waiting for customer"
  166.     End If
  167. End Sub
  168.