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 / ch13 / frmserv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.2 KB  |  161 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. Private Sub chkOpen_Click()
  72.     Dim usename$
  73.     Dim InitialStand As CheckStand
  74.     Dim written&
  75.     ' Note lack of error checking here
  76.     usename$ = "ChkStd" & txtCheck.Text
  77.     If chkOpen.value = 1 Then
  78.         If FileHandle <> 0 Then Exit Sub
  79.         ' Create new file, read write access, exclusive use,
  80.         FileHandle = CreateFile(usename$, GENERIC_READ Or GENERIC_WRITE, _
  81.                                 0, Security, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_RANDOM_ACCESS, 0)
  82.         If FileHandle = -1 Then
  83.             MsgBox "Can't create file"
  84.             FileHandle = 0
  85.             Exit Sub
  86.         End If
  87.         ' Write the initial file info
  88.         Call WriteFile(FileHandle, InitialStand, Len(InitialStand), written, 0)
  89.         Call FlushFileBuffers(FileHandle)
  90.         ' Now we need a mapping
  91.         MappingHandle = CreateFileMapping(FileHandle, Security, PAGE_READWRITE, 0, 0, usename$ & "map")
  92.         If MappingHandle = 0 Then
  93.             MsgBox "Can't create file mapping"
  94.             Exit Sub
  95.         End If
  96.         MappingAddress = MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0)
  97.         If MappingAddress = 0 Then
  98.             MsgBox "Can't map view of the file"
  99.             Exit Sub
  100.         End If
  101.         Timer1.Enabled = True   ' Start watching for customers
  102.     Else
  103.         CleanUp
  104.         lblStatus.Caption = "Closed"
  105.     End If
  106. End Sub
  107. Private Sub Form_Load()
  108.     With Security
  109.         .nLength = Len(Security)
  110.         .lpSecurityDescriptor = 0
  111.         .bInheritHandle = True   ' Doesn't really matter
  112.     End With
  113. End Sub
  114. Private Sub Form_Unload(Cancel As Integer)
  115.     ' Remember, this won't get called if you Stop without
  116.     ' closing the main window.
  117.     CleanUp
  118. End Sub
  119. Private Sub CleanUp()
  120.     ' Remember, this won't get called if you Stop without
  121.     ' closing the main window.
  122.     Timer1.Enabled = False
  123.     If MappingAddress <> 0 Then
  124.         Call UnmapViewOfFile(MappingAddress)
  125.         MappingAddress = 0
  126.     End If
  127.     If MappingHandle <> 0 Then
  128.         Call CloseHandle(MappingHandle)
  129.         MappingHandle = 0
  130.     End If
  131.     If FileHandle <> 0 Then
  132.         Call CloseHandle(FileHandle)
  133.         FileHandle = 0
  134.     End If
  135. End Sub
  136. Private Sub Timer1_Timer()
  137.     Dim cs As CheckStand
  138.     Static item%
  139.     Static tot As Single
  140.     ' Look at the checkstand
  141.     agCopyData ByVal MappingAddress, cs, Len(cs)
  142.     If cs.Done Then
  143.         If cs.Total <> 0 Then Exit Sub ' Waiting for payment
  144.         If item = 0 Then lblStatus.Caption = "Checking out " & cs.Client
  145.         tot = tot + cs.Prices(item)
  146.         item = item + 1
  147.         ' Stop at free item anyway
  148.         If item > UBound(cs.Prices) Or cs.Prices(item - 1) = 0 Then ' Done
  149.             lstCustomers.AddItem cs.Client & " " & Format$(tot, "0.00")
  150.             cs.Total = tot
  151.             cs.Done = 0
  152.             item = 0
  153.             tot = 0
  154.             lblStatus.Caption = "Waiting to pay"
  155.         End If
  156.         agCopyData cs, ByVal MappingAddress, Len(cs)
  157.     Else
  158.         lblStatus.Caption = "Waiting for customer"
  159.     End If
  160. End Sub
  161.