-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnnamedPipe.cpp
More file actions
66 lines (60 loc) · 1.62 KB
/
UnnamedPipe.cpp
File metadata and controls
66 lines (60 loc) · 1.62 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
#include "UnnamedPipe.h"
#include <exception>
UnnamedPipe::UnnamedPipe()
{
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof( SECURITY_ATTRIBUTES );
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if( !CreatePipe( &_outRdHandle, &_outWrHandle, &saAttr, 0 ) )
{
throw std::exception( "OutPipe failed!" );
}
if( !SetHandleInformation( _outRdHandle, HANDLE_FLAG_INHERIT, 0 ) )
{
throw std::exception( "OutPipe inherited!" );
}
if( !CreatePipe( &_inRdHandle, &_inWrHandle, &saAttr, 0 ) )
{
throw std::exception( "InPipe failed!" );
}
if( !SetHandleInformation( _inWrHandle, HANDLE_FLAG_INHERIT, 0 ) )
{
throw std::exception( "InPipe inherited!" );
}
}
UnnamedPipe::~UnnamedPipe()
{
CloseHandle( _inRdHandle );
CloseHandle( _inWrHandle );
CloseHandle( _outRdHandle );
CloseHandle( _outWrHandle );
}
HANDLE UnnamedPipe::getProcInHandle()
{
return _inRdHandle;
}
HANDLE UnnamedPipe::getProcOutHandle()
{
return _outWrHandle;
}
void UnnamedPipe::writeToPipe( std::string data )
{
DWORD written = 0;
BOOL bSuccess = WriteFile( _inWrHandle, data.c_str(), (DWORD)data.length() + 1, &written, NULL );
if( !bSuccess )
{
throw std::exception( "Writing to pipe failed!" );
}
}
std::string UnnamedPipe::readFromPipe()
{
DWORD dwRead;
BOOL bSuccess = ReadFile( _outRdHandle, chBuf, 4096, &dwRead, NULL );
chBuf[dwRead] = '\0';
if( !bSuccess || dwRead == 0 )
{
throw std::exception( "Reading from pipe failed" );
}
return std::string( chBuf );
}