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