-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipe.h
More file actions
163 lines (130 loc) · 4.04 KB
/
Copy pathPipe.h
File metadata and controls
163 lines (130 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/****************************** Module Header ******************************\
* Module Name: CppNamedPipeClient.cpp
* Project: CppNamedPipeClient
* Copyright (c) Microsoft Corporation.
*
* Named pipe is a mechanism for one-way or bi-directional inter-process
* communication between the pipe server and one or more pipe clients in the
* local machine or across the computers in the intranet:
*
* PIPE_ACCESS_INBOUND:
* Client (GENERIC_WRITE) ---> Server (GENERIC_READ)
*
* PIPE_ACCESS_OUTBOUND:
* Client (GENERIC_READ) <--- Server (GENERIC_WRITE)
*
* PIPE_ACCESS_DUPLEX:
* Client (GENERIC_READ or GENERIC_WRITE, or both) <-->
* Server (GENERIC_READ and GENERIC_WRITE)
*
* This sample demonstrates a named pipe client that attempts to connect to
* the pipe server, \\.\pipe\HelloWorld, with the GENERIC_READ and
* GENERIC_WRITE permissions. The client writes a message to the pipe server
* and receive its response.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
*
* History:
* * 1/11/2009 11:20 PM Jialiang Ge Created
\***************************************************************************/
#pragma once
#undef _UNICODE
#undef UNICODE
#pragma region Includes
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma endregion
using namespace std;
#define BUFFER_SIZE 1024 // 1K
class Pipe
{
private:
HANDLE hPipe;
LPTSTR strPipeName;
public:
Pipe()
{
// Prepare the pipe name
strPipeName = TEXT("\\\\.\\pipe\\chessPipe");
}
bool connect()
{
hPipe = CreateFile(
strPipeName, // Pipe name
GENERIC_READ | // Read and write access
GENERIC_WRITE,
0, // No sharing
NULL, // Default security attributes
OPEN_EXISTING, // Opens existing pipe
0, // Default attributes
NULL); // No template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
return true;
if (// Exit if an error other than ERROR_PIPE_BUSY occurs
GetLastError() != ERROR_PIPE_BUSY
||
// All pipe instances are busy, so wait for 5 seconds
!WaitNamedPipe(strPipeName, 5000))
{
_tprintf(_T("Unable to open named pipe %s w/err 0x%08lx\n"),
strPipeName, GetLastError());
return false;
}
_tprintf(_T("The named pipe, %s, is connected.\n"), strPipeName);
return true;
}
bool sendMessageToGraphics(char* msg)
{
//char ea[] = "SSS";
char* chRequest = msg; // Client -> Server
DWORD cbBytesWritten, cbRequestBytes;
// Send one message to the pipe.
cbRequestBytes = sizeof(TCHAR) * (lstrlen(chRequest) + 1);
BOOL bResult = WriteFile( // Write to the pipe.
hPipe, // Handle of the pipe
chRequest, // Message to be written
cbRequestBytes, // Number of bytes to write
&cbBytesWritten, // Number of bytes written
NULL); // Not overlapped
if (!bResult/*Failed*/ || cbRequestBytes != cbBytesWritten/*Failed*/)
{
_tprintf(_T("WriteFile failed w/err 0x%08lx\n"), GetLastError());
return false;
}
_tprintf(_T("Sends %ld bytes; Message: \"%s\"\n"),
cbBytesWritten, chRequest);
return true;
}
string getMessageFromGraphics()
{
DWORD cbBytesRead;
DWORD cbReplyBytes;
TCHAR chReply[BUFFER_SIZE]; // Server -> Client
cbReplyBytes = sizeof(TCHAR) * BUFFER_SIZE;
BOOL bResult = ReadFile( // Read from the pipe.
hPipe, // Handle of the pipe
chReply, // Buffer to receive the reply
cbReplyBytes, // Size of buffer
&cbBytesRead, // Number of bytes read
NULL); // Not overlapped
if (!bResult && GetLastError() != ERROR_MORE_DATA)
{
_tprintf(_T("ReadFile failed w/err 0x%08lx\n"), GetLastError());
return "";
}
_tprintf(_T("Receives %ld bytes; Message: \"%s\"\n"),
cbBytesRead, chReply);
return chReply;
}
void close()
{
CloseHandle(hPipe);
}
};