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 / samples4 / ch13 / frmserv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.2 KB  |  162 lines

  1. VERSION 4.00
  2. Begin VB.Form frmServer 
  3.    Caption         =   "CheckStand"
  4.    ClientHeight    =   2865
  5.    ClientLeft      =   5475
  6.    ClientTop       =   1770
  7.    ClientWidth     =   4200
  8.    Height          =   3270
  9.    Left            =   5415
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   2865
  12.    ScaleWidth      =   4200
  13.    Top             =   1425
  14.    Width           =   4320
  15.    Begin VB.ListBox lstCustomers 
  16.       Height          =   1395
  17.       ItemData        =   "FRMSERV.frx":0000
  18.       Left            =   300
  19.       List            =   "FRMSERV.frx":0007
  20.       TabIndex        =   3
  21.       Top             =   1200
  22.       Width           =   3495
  23.    End
  24.    Begin VB.Timer Timer1 
  25.       Enabled         =   0   'False
  26.       Interval        =   400
  27.       Left            =   -120
  28.       Top             =   780
  29.    End
  30.    Begin VB.CheckBox chkOpen 
  31.       Caption         =   "Open"
  32.       Height          =   255
  33.       Left            =   2460
  34.       TabIndex        =   2
  35.       Top             =   240
  36.       Width           =   915
  37.    End
  38.    Begin VB.TextBox txtCheck 
  39.       Height          =   315
  40.       Left            =   1380
  41.       TabIndex        =   0
  42.       Text            =   "1"
  43.       Top             =   180
  44.       Width           =   795
  45.    End
  46.    Begin VB.Label lblStatus 
  47.       Height          =   195
  48.       Left            =   360
  49.       TabIndex        =   4
  50.       Top             =   780
  51.       Width           =   3495
  52.    End
  53.    Begin VB.Label Label1 
  54.       Alignment       =   1  'Right Justify
  55.       Caption         =   "Checkstand:"
  56.       Height          =   255
  57.       Left            =   180
  58.       TabIndex        =   1
  59.       Top             =   240
  60.       Width           =   1095
  61.    End
  62. Attribute VB_Name = "frmServer"
  63. Attribute VB_Creatable = False
  64. Attribute VB_Exposed = False
  65. Option Explicit
  66. ' Copyright 
  67.  1997 by Desaware Inc. All Rights Reserved
  68. Dim FileHandle As Long      ' Handle of open file
  69. Dim MappingHandle As Long   ' Handle to file mapping
  70. Dim MappingAddress As Long  ' Address of file mapping
  71. Dim Security As SECURITY_ATTRIBUTES
  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.         Timer1.Enabled = True   ' Start watching for customers
  103.     Else
  104.         CleanUp
  105.         lblStatus.Caption = "Closed"
  106.     End If
  107. End Sub
  108. Private Sub Form_Load()
  109.     With Security
  110.         .nLength = Len(Security)
  111.         .lpSecurityDescriptor = 0
  112.         .bInheritHandle = True   ' Doesn't really matter
  113.     End With
  114. End Sub
  115. Private Sub Form_Unload(Cancel As Integer)
  116.     ' Remember, this won't get called if you Stop without
  117.     ' closing the main window.
  118.     CleanUp
  119. End Sub
  120. Private Sub CleanUp()
  121.     ' Remember, this won't get called if you Stop without
  122.     ' closing the main window.
  123.     Timer1.Enabled = False
  124.     If MappingAddress <> 0 Then
  125.         Call UnmapViewOfFile(MappingAddress)
  126.         MappingAddress = 0
  127.     End If
  128.     If MappingHandle <> 0 Then
  129.         Call CloseHandle(MappingHandle)
  130.         MappingHandle = 0
  131.     End If
  132.     If FileHandle <> 0 Then
  133.         Call CloseHandle(FileHandle)
  134.         FileHandle = 0
  135.     End If
  136. End Sub
  137. Private Sub Timer1_Timer()
  138.     Dim cs As CheckStand
  139.     Static item%
  140.     Static tot As Single
  141.     ' Look at the checkstand
  142.     agCopyData ByVal MappingAddress, cs, Len(cs)
  143.     If cs.Done Then
  144.         If cs.Total <> 0 Then Exit Sub ' Waiting for payment
  145.         If item = 0 Then lblStatus.Caption = "Checking out " & cs.Client
  146.         tot = tot + cs.Prices(item)
  147.         item = item + 1
  148.         ' Stop at free item anyway
  149.         If item > UBound(cs.Prices) Or cs.Prices(item - 1) = 0 Then ' Done
  150.             lstCustomers.AddItem cs.Client & " " & Format$(tot, "0.00")
  151.             cs.Total = tot
  152.             cs.Done = 0
  153.             item = 0
  154.             tot = 0
  155.             lblStatus.Caption = "Waiting to pay"
  156.         End If
  157.         agCopyData cs, ByVal MappingAddress, Len(cs)
  158.     Else
  159.         lblStatus.Caption = "Waiting for customer"
  160.     End If
  161. End Sub
  162.