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 / pipesrv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.6 KB  |  185 lines

  1. VERSION 5.00
  2. Begin VB.Form frmserver 
  3.    Caption         =   "Named Pipe Server"
  4.    ClientHeight    =   2175
  5.    ClientLeft      =   1125
  6.    ClientTop       =   1515
  7.    ClientWidth     =   4230
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   2175
  11.    ScaleWidth      =   4230
  12.    Begin VB.CommandButton cmdAsync2 
  13.       Caption         =   "Send Async2"
  14.       Height          =   435
  15.       Left            =   2520
  16.       TabIndex        =   4
  17.       Top             =   1560
  18.       Width           =   1455
  19.    End
  20.    Begin VB.CommandButton sndAsync 
  21.       Caption         =   "Send Async"
  22.       Height          =   435
  23.       Left            =   2520
  24.       TabIndex        =   3
  25.       Top             =   1020
  26.       Width           =   1455
  27.    End
  28.    Begin VB.CommandButton sndBlocked 
  29.       Caption         =   "Send Blocked"
  30.       Height          =   435
  31.       Left            =   240
  32.       TabIndex        =   1
  33.       Top             =   1020
  34.       Width           =   1395
  35.    End
  36.    Begin VB.Timer Timer1 
  37.       Interval        =   50
  38.       Left            =   2940
  39.       Top             =   180
  40.    End
  41.    Begin VB.Label lblStat 
  42.       Caption         =   "Idle"
  43.       Height          =   195
  44.       Left            =   240
  45.       TabIndex        =   2
  46.       Top             =   660
  47.       Width           =   1755
  48.    End
  49.    Begin VB.Label Label1 
  50.       Caption         =   "Label1"
  51.       Height          =   255
  52.       Left            =   240
  53.       TabIndex        =   0
  54.       Top             =   240
  55.       Width           =   1635
  56.    End
  57. Attribute VB_Name = "frmserver"
  58. Attribute VB_GlobalNameSpace = False
  59. Attribute VB_Creatable = False
  60. Attribute VB_PredeclaredId = True
  61. Attribute VB_Exposed = False
  62. Option Explicit
  63. ' Copyright 
  64.  1997 by Desaware Inc. All Rights Reserved
  65. Dim ctr&
  66. Dim pipehnd&
  67. Dim buffer$
  68. Dim ol As OVERLAPPED
  69. Dim ol2 As OVERLAPPED
  70. Dim callbackobj As Object
  71. Private Sub cmdAsync2_Click()
  72.     Dim res&
  73.     Dim written&
  74.     Set callbackobj = CreateObject("dwWatcher.dwSyncWatch")
  75.     ' Wait for a client to appear
  76.     cmdAsync2.Enabled = False
  77.     ol2.hEvent = CreateEvent(0, True, False, vbNullString)
  78.     If ol2.hEvent = 0 Then
  79.         MsgBox "Can't create event"
  80.         Exit Sub
  81.     End If
  82.     lblStat.Caption = "Waiting for client"
  83.     lblStat.Refresh
  84.     res = ConnectNamedPipe(pipehnd, 0)
  85.     If res <> 0 Or (res = 0 And GetLastError() = ERROR_PIPE_CONNECTED) Then
  86.         lblStat.Caption = "Sending data"
  87.         lblStat.Refresh
  88.         ' Pipe is connected
  89.         buffer$ = String$(200, 0)
  90.         res = WriteFileAsync(pipehnd, ByVal buffer, 200, written, ol2)
  91.         ' Turn on the callback object
  92.         Call callbackobj.SetAppCallback(Me)
  93.         Call callbackobj.SetObjectWatch(GetCurrentProcessId(), ol2.hEvent)
  94.     Else
  95.         Call CloseHandle(ol2.hEvent)
  96.         ol2.hEvent = 0
  97.         MsgBox "Client has disconnected"
  98.         lblStat.Caption = "Idle"
  99.         cmdAsync2.Enabled = True
  100.     End If
  101. End Sub
  102. Private Sub Form_Load()
  103.     pipehnd& = CreateNamedPipe("\\.\pipe\vbpgpipe1", PIPE_ACCESS_OUTBOUND Or FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 0, 0, 0, 0)
  104.     If pipehnd = 0 Then
  105.         sndBlocked.Enabled = False
  106.         sndAsync.Enabled = False
  107.         cmdAsync2.Enabled = False
  108.         lblStat.Caption = "Can't create pipe"
  109.     End If
  110. End Sub
  111. Private Sub Form_Unload(Cancel As Integer)
  112.     If pipehnd& <> 0 Then
  113.         Call CloseHandle(pipehnd)
  114.     End If
  115. End Sub
  116. Private Sub sndAsync_Click()
  117.     Dim res&
  118.     Dim written&
  119.     ' Wait for a client to appear
  120.     sndAsync.Enabled = False
  121.     ol.hEvent = CreateEvent(0, True, False, vbNullString)
  122.     If ol.hEvent = 0 Then
  123.         MsgBox "Can't create event"
  124.         Exit Sub
  125.     End If
  126.     lblStat.Caption = "Waiting for client"
  127.     lblStat.Refresh
  128.     res = ConnectNamedPipe(pipehnd, 0)
  129.     If res <> 0 Or (res = 0 And GetLastError() = ERROR_PIPE_CONNECTED) Then
  130.         lblStat.Caption = "Sending data"
  131.         lblStat.Refresh
  132.         ' Pipe is connected
  133.         buffer$ = String$(200, 0)
  134.         res = WriteFileAsync(pipehnd, ByVal buffer, 200, written, ol)
  135.     Else
  136.         Call CloseHandle(ol2.hEvent)
  137.         ol2.hEvent = 0
  138.         MsgBox "Client has disconnected"
  139.         lblStat.Caption = "Idle"
  140.         sndAsync.Enabled = True
  141.     End If
  142. End Sub
  143. Private Sub sndBlocked_Click()
  144.     Dim res&
  145.     Dim written&
  146.     ' Wait for a client to appear
  147.     sndBlocked.Enabled = False
  148.     lblStat.Caption = "Waiting for client"
  149.     lblStat.Refresh
  150.     res = ConnectNamedPipe(pipehnd, 0)
  151.     If res <> 0 Or (res = 0 And GetLastError() = ERROR_PIPE_CONNECTED) Then
  152.         lblStat.Caption = "Sending data"
  153.         lblStat.Refresh
  154.         ' Pipe is connected
  155.         buffer$ = String$(200, 0)
  156.         res = WriteFile(pipehnd, ByVal buffer, 200, written, 0)
  157.     Else
  158.         MsgBox "Client has disconnected"
  159.     End If
  160.     lblStat.Caption = "Idle"
  161.     sndBlocked.Enabled = True
  162. End Sub
  163. Private Sub Timer1_Timer()
  164.     Dim res&
  165.     Label1.Caption = ctr&
  166.     ctr& = ctr& + 1
  167.     If ol.hEvent <> 0 Then
  168.         ' Async operation is in progress
  169.         res = WaitForSingleObject(ol.hEvent, 0)
  170.         If res = WAIT_OBJECT_0 Then
  171.             ' Async read is done
  172.             Call CloseHandle(ol.hEvent)
  173.             ol.hEvent = 0
  174.             lblStat.Caption = "Idle"
  175.             sndAsync.Enabled = True
  176.         End If
  177.     End If
  178. End Sub
  179. Public Sub dwSignaled(o As Object)
  180.     Call CloseHandle(ol2.hEvent)
  181.     ol2.hEvent = 0
  182.     lblStat.Caption = "Idle"
  183.     cmdAsync2.Enabled = True
  184. End Sub
  185.