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

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