bool MWinNamedPipeServer::Connect(void) { if(mClientConnected==true) { DisconnectNamedPipe(mhPipe); } if(ConnectNamedPipe(mhPipe,NULL)==FALSE) { // It is possible that a client connects before the ConnectNamedPipe is invoked after CreateNamed Pipe. // Connections is still good! // Ref: https://msdn.microsoft.com/query/dev15.query?appId=Dev15IDEF1&l=EN-US&k=k(NAMEDPIPEAPI%2FConnectNamedPipe);k(ConnectNamedPipe);k(DevLang-C%2B%2B);k(TargetOS-Windows)&rd=true DWORD error=GetLastError(); if(error==ERROR_PIPE_CONNECTED) { mClientConnected=true; return true; } mClientConnected=false; return false; } mClientConnected=true; return true; }
BOOL NamedPipeInputFuzzer::Init() { BOOL bResult=FALSE; dibf_pipe = CreateNamedPipe(_T("\\\\.\\pipe\\dibf_pipe"), PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT|PIPE_REJECT_REMOTE_CLIENTS, 1, MAX_BUFSIZE/2, MAX_BUFSIZE/2, 0, NULL); if(dibf_pipe!=INVALID_HANDLE_VALUE) { TPRINT(VERBOSITY_DEFAULT, _T("Named pipe created, waiting for connection...\n")); if(ConnectNamedPipe(dibf_pipe, NULL)?TRUE:(GetLastError()==ERROR_PIPE_CONNECTED)) { TPRINT(VERBOSITY_DEFAULT, _T("Fuzzing client connected to named pipe\n")); inputThread = CreateThread(NULL, 0, FuzzInputProc, this, 0, NULL); if(inputThread) { bResult = TRUE; } else { TPRINT(VERBOSITY_ERROR, _T("Failed to create fuzz input thread with error %#.8x\n"), GetLastError()); } } } return bResult; }
static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc) { if (npc->listening) return RPC_S_OK; npc->listening = TRUE; for (;;) { if (ConnectNamedPipe(npc->pipe, &npc->ovl)) return RPC_S_OK; switch(GetLastError()) { case ERROR_PIPE_CONNECTED: SetEvent(npc->ovl.hEvent); return RPC_S_OK; case ERROR_IO_PENDING: /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */ return RPC_S_OK; case ERROR_NO_DATA_DETECTED: /* client has disconnected, retry */ DisconnectNamedPipe( npc->pipe ); break; default: npc->listening = FALSE; WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError()); return RPC_S_OUT_OF_RESOURCES; } } }
// References: // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx static HANDLE connect_pipe(const char* app_name) { HANDLE pipe = INVALID_HANDLE_VALUE; char username[UNLEN + 1]; DWORD unlen = UNLEN + 1; if (GetUserNameA(username, &unlen)) { // add username to the pipe path so it will not clash with other users' pipes. char pipe_name[MAX_PATH]; sprintf(pipe_name, "\\\\.\\pipe\\%s\\%s_pipe", username, app_name); const size_t buffer_size = 1024; // create the pipe pipe = CreateNamedPipeA(pipe_name, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, buffer_size, buffer_size, NMPWAIT_USE_DEFAULT_WAIT, &g_securityAttributes); if (pipe != INVALID_HANDLE_VALUE) { // try to connect to the named pipe // NOTE: this is a blocking call if (FALSE == ConnectNamedPipe(pipe, NULL)) { // fail to connect the pipe CloseHandle(pipe); pipe = INVALID_HANDLE_VALUE; } } } return pipe; }
int win32_fifo_mkfifo(const char *path) { HANDLE ret; win32_fifo_close(); #ifdef WANT_WIN32_UNICODE wchar_t *str; if(win32_utf8_wide(path,&str,NULL) == 0) { fprintf(stderr,"Cannot get FIFO name, likely out of memory\n"); return -1; } #if (DEBUG == 1) fwprintf(stderr,L"CreateNamedPipeW %ws\n", str); #endif ret = CreateNamedPipeW(str,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE,1,255,255,0,NULL); free(str); #else #if (DEBUG == 1) fprintf(stderr,"CreateNamedPipeA %s\n", path); #endif ret = CreateNamedPipeA(path,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE,1,255,255,0,NULL); #endif /* WANT_WIN32_UNICODE */ if(ret == INVALID_HANDLE_VALUE) return -1; fifohandle = ret; /* Wait for client */ ConnectNamedPipe(fifohandle,&ov1); WaitForSingleObjectEx(fifohandle,INFINITE,TRUE); return 0; }
bool ezPipeChannel_win::ProcessConnection() { EZ_ASSERT_DEBUG(m_ThreadId == ezThreadUtils::GetCurrentThreadID(), "Function must be called from worker thread!"); if (m_InputState.IsPending) m_InputState.IsPending = false; BOOL res = ConnectNamedPipe(m_PipeHandle, &m_InputState.Context.Overlapped); if (res) { //EZ_REPORT_FAILURE return false; } ezUInt32 error = GetLastError(); switch (error) { case ERROR_IO_PENDING: m_InputState.IsPending = true; break; case ERROR_PIPE_CONNECTED: m_Connected = true; break; case ERROR_NO_DATA: return false; default: ezLog::Error("Could not connect to pipe (Error code: {0})", ezArgErrorCode(error)); return false; } return true; }
// Communicaton Thread Pool, handles the incoming xCmd.exe requests void CommunicationPoolThread(PVOID) { HANDLE hPipe = NULL; for (;;) { SECURITY_ATTRIBUTES SecAttrib = {0}; SECURITY_DESCRIPTOR SecDesc; InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(&SecDesc, TRUE, NULL, TRUE); SecAttrib.nLength = sizeof(SECURITY_ATTRIBUTES); SecAttrib.lpSecurityDescriptor = &SecDesc;; SecAttrib.bInheritHandle = TRUE; // Create communication pipe hPipe = CreateNamedPipe( _T("\\\\.\\pipe\\")XCMDCOMM, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, (DWORD)-1, &SecAttrib); if ( hPipe != NULL ) { // Waiting for client to connect to this pipe ConnectNamedPipe( hPipe, NULL ); _beginthread( CommunicationPipeThreadProc, 0, (void*)hPipe); } } }
DWORD ChildProcess::ListenerThread() { // wait for someone to connect to the pipe if (ConnectNamedPipe(hPipe_, NULL) || GetLastError() == ERROR_PIPE_CONNECTED) { // Acquire the lock while writing to processOutput_ char buffer[1024]; DWORD dwRead; while (ReadFile(hPipe_, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE) { if (dwRead > 0) { CSingleLock locker(&outputLock_); buffer[dwRead] = 0; OutputDebugStringA(buffer); processOutput_ += AnsiToUnicode(buffer); } SetEvent(hOutputAvailable_); } } else { OutputDebugString(L"Connect failed.\n"); } DisconnectNamedPipe(hPipe_); return 0; }
void CWE253_Incorrect_Check_of_Function_Return_Value__char_w32CreateNamedPipe_01_bad() { { char * pipeName = "\\\\.\\pipe\\mypipe"; HANDLE hPipe = INVALID_HANDLE_VALUE; BOOL fConnected = FALSE; hPipe = CreateNamedPipeA( pipeName, FILE_FLAG_FIRST_PIPE_INSTANCE, /* FILE_FLAG_FIRST_PIPE_INSTANCE - this flag must be set */ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, NMPWAIT_USE_DEFAULT_WAIT, NULL); /* FLAW: If CreateNamedPipeA() failed, the return value will be INVALID_HANDLE_VALUE, but we are checking to see if the return value is NULL */ if (hPipe == NULL) { exit(1); } fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); /* We'll leave out most of the implementation since it has nothing to do with the CWE * and since the checkers are looking for certain function calls anyway */ CloseHandle(hPipe); } }
static void good1() { { char * pipeName = "\\\\.\\pipe\\mypipe"; HANDLE hPipe = INVALID_HANDLE_VALUE; BOOL fConnected = FALSE; hPipe = CreateNamedPipeA( pipeName, FILE_FLAG_FIRST_PIPE_INSTANCE, /* FILE_FLAG_FIRST_PIPE_INSTANCE - this flag must be set */ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, NMPWAIT_USE_DEFAULT_WAIT, NULL); /* FIX: check for the correct return value */ if (hPipe == INVALID_HANDLE_VALUE) { exit(1); } fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); /* We'll leave out most of the implementation since it has nothing to do with the CWE * and since the checkers are looking for certain function calls anyway */ CloseHandle(hPipe); } }
void NamedPipe::Initialize() { SECURITY_ATTRIBUTES attr; attr.bInheritHandle = TRUE; attr.nLength = sizeof(SECURITY_ATTRIBUTES); attr.lpSecurityDescriptor = NULL; mHandle = CreateFileA( mName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, &attr, OPEN_EXISTING, 0, NULL); if (mHandle == INVALID_HANDLE) { mHandle = CreateNamedPipeA( mName.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 65536, 65536, NMPWAIT_WAIT_FOREVER, &attr); if (mHandle == INVALID_HANDLE) { throw NamedPipeException(GetLastErrorString()); } else if (!ConnectNamedPipe(mHandle, NULL) && GetLastError() != ERROR_PIPE_CONNECTED) { throw NamedPipeException(GetLastErrorString()); } } }
void CWE252_Unchecked_Return_Value__char_w32CreateNamedPipe_04_bad() { if(STATIC_CONST_TRUE) { { char * pipeName = "\\\\.\\pipe\\mypipe"; HANDLE hPipe = INVALID_HANDLE_VALUE; BOOL fConnected = FALSE; hPipe = CreateNamedPipeA( pipeName, FILE_FLAG_FIRST_PIPE_INSTANCE, /* FILE_FLAG_FIRST_PIPE_INSTANCE - this flag must be set */ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, NMPWAIT_USE_DEFAULT_WAIT, NULL); /* FLAW: Do not check the return value */ fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); /* We'll leave out most of the implementation since it has nothing to do with the CWE * and since the checkers are looking for certain function calls anyway */ CloseHandle(hPipe); } } }
HRESULT PipeStream::OnCustomRequested( HandleStream::AsyncContext* handle_context) { auto context = static_cast<AsyncContext*>(handle_context); BOOL succeeded; switch (context->type) { case PipeRequest::WaitForConnection: succeeded = ConnectNamedPipe(handle_, context); break; case PipeRequest::Transact: succeeded = TransactNamedPipe(handle_, context->buffer, context->length, context->buffer2, context->length2, nullptr, context); break; default: assert(false); return E_NOTIMPL; } DWORD error = GetLastError(); if (!succeeded && error != ERROR_IO_PENDING) { if (error == ERROR_PIPE_CONNECTED && context->hEvent != NULL) SetEvent(context->hEvent); return __HRESULT_FROM_WIN32(error); } return S_OK; }
Piper::Piper() { // Get tibias main module for future use baseAddress = (DWORD) GetModuleHandle(0); // Define a pipe name pipeName = L"\\\\.\\pipe\\piperpipe"; // Create the pipe hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE, 1, BUFFER_SIZE, BUFFER_SIZE, 5000, NULL); // If we didn't make it, tell the user something went wrong. if (hPipe == NULL || hPipe == INVALID_HANDLE_VALUE) { MessageBox(NULL, L"Error loading named pipe, please consult your software provider for support", L"Error!", MB_ICONSTOP); } // Wait for a client to connect to see the data down the pipe. ConnectNamedPipe(hPipe, NULL); // Send a message down the pipe to confirm it's working Send("SYSTEM OPERATIONAL"); // Send the base address for good measure char *buff = new char[32]; _itoa_s(baseAddress, buff, strlen(buff), 16); Send(buff); for (int i = 0; i < 10; i++) { Send("Testing"); Sleep(1000); } }
shared_ptr<InterprocessIo> InterprocessServer::Connect() { shared_handle hPipe(CreateNamedPipe( GetPipeName(), PIPE_ACCESS_DUPLEX | // read/write access FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | // message type pipe PIPE_READMODE_MESSAGE | // message-read mode PIPE_WAIT, // non-blocking mode PIPE_UNLIMITED_INSTANCES, // max. instances BUF_SIZE, // output buffer size BUF_SIZE, // input buffer size NMPWAIT_USE_DEFAULT_WAIT, // client time-out NULL) // NULL DACL ); if(!hPipe) { return shared_ptr<InterprocessIo>(); } if(!ConnectNamedPipe(hPipe.get(), NULL) && (GetLastError()!=ERROR_PIPE_CONNECTED)) { return shared_ptr<InterprocessIo>(); } return shared_ptr<InterprocessIo>(new InterprocessIo(hPipe)); }
bool GWCAServer::ListenConnections() { bool bConnected = false; HANDLE hPipe = NULL; port.Create(1); while (true) { Sleep(0); hPipe = CreateNamedPipe( m_cPipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, 1000, NULL); bConnected = (ConnectNamedPipe(hPipe, 0) == 1) ? true : (GetLastError() == ERROR_PIPE_CONNECTED); if(bConnected){ port.AssociateFile(hPipe, (ULONG_PTR)hPipe); LPPIPEINST pOverlapped = (LPPIPEINST)GlobalAlloc(GPTR, sizeof(PIPEINST)); ReadFile(hPipe, &pOverlapped->bmRequest, sizeof(pOverlapped->bmRequest), &pOverlapped->cbRead, (LPOVERLAPPED)pOverlapped); }else{ CloseHandle(hPipe); } } return true; }
void __stdcall Watcher::pipeThread(Watcher *w) { for (;;) { HANDLE pipe = w->pipe; WORD rv, myv = Base::instance()->getNVersion(); DWORD rd; WORD cmd; if (!ConnectNamedPipe(pipe, NULL)) { Sleep(1000); continue; } ReadFile(pipe, &cmd, sizeof(cmd), &rd, NULL); WriteFile(pipe, &myv, sizeof(myv), &rd, NULL); if (!cmd) { ReadFile(pipe, &rv, sizeof(rv), &rd, NULL); if (HIBYTE(rv) > HIBYTE(myv) || (HIBYTE(rv) == HIBYTE(myv) && LOBYTE(rv) > LOBYTE(myv))) { w->terminate(); return; } } else { char file[MAX_PATH + 1]; ReadFile(pipe, file, sizeof(file), &rd, NULL); file[MAX_PATH] = 0; log(LL_DIAG, L_NEW L_DEL L_COMMAND "%s", file); Sleep(6000); Base::instance()->forceDeleteFile(std::string(file)); } DisconnectNamedPipe(pipe); } }
static unsigned int __stdcall _ecore_con_local_win32_listening(void *data) { Ecore_Con_Server *svr; BOOL res; svr = (Ecore_Con_Server *)data; while (1) { res = ConnectNamedPipe(svr->pipe, NULL); if (!res) { ERR("Opening the connection to the client failed"); CloseHandle(svr->pipe); svr->pipe = NULL; } break; } DBG("Client connected"); printf(" ### %s\n", __FUNCTION__); _endthreadex(0); return 0; }
unsigned WINAPI pipe_listener(void *parameters) { while(1) { HANDLE pipe = CreateNamedPipe( pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, PIPE_BUFSIZE, PIPE_BUFSIZE, 0, NULL); BOOL clientConnected; if( pipe == INVALID_HANDLE_VALUE ) { printf("Error creating named pipe. Code %lu\n", GetLastError()); return -1; } // True if client connects *after* server called ConnectNamedPipe // or *between* CreateNamedPipe and ConnectNamedPipe clientConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : GetLastError()==ERROR_PIPE_CONNECTED; printf("Client connected!\n"); if( !clientConnected ) { printf("Failure while listening for clients. Code %lu\n", GetLastError()); CloseHandle(pipe); return -1; } printf("Create client thread\n"); _beginthreadex(NULL, 0, fifo_rx, (PVOID)pipe, 0, 0); } return 0; }
void WinListener::create_pipe() { _pipe = CreateNamedPipe(_pipename, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, PIPE_BUF_SIZE, PIPE_BUF_SIZE, PIPE_TIMEOUT, NULL); if (_pipe == INVALID_HANDLE_VALUE) { THROW("CreateNamedPipe() failed %u", GetLastError()); } if (ConnectNamedPipe(_pipe, &_overlap)) { THROW("ConnectNamedPipe() is not pending"); } switch (GetLastError()) { case ERROR_IO_PENDING: DBG(0, "Pipe waits for connection"); break; case ERROR_PIPE_CONNECTED: { DBG(0, "Pipe already connected"); WinConnection *con = new WinConnection(_pipe, _process_loop); NamedPipe::ConnectionInterface &con_interface = _listener_interface.create(); con->set_handler(&con_interface); con_interface.bind((NamedPipe::ConnectionRef)con); create_pipe(); break; } default: THROW("ConnectNamedPipe() failed %u", GetLastError()); } }
static int npipe_input_open(const char *pipe_name) { static OVERLAPPED overlapped; static HANDLE hEvent; char PipeName[256]; DWORD ret; hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); memset( &overlapped, 0, sizeof(OVERLAPPED)); overlapped.hEvent = hEvent; sprintf(PipeName, "\\\\.\\pipe\\%s", pipe_name); hPipe = CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED, // PIPE_WAIT| PIPE_READMODE_BYTE |PIPE_TYPE_BYTE, 2, 0, PIPE_BUFFER_SIZE, 0, NULL); if (hPipe == INVALID_HANDLE_VALUE) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't create Named Pipe %s : %ld", pipe_name, GetLastError()); return -1; } ret = ConnectNamedPipe(hPipe, &overlapped); if ( (ret == 0) && (ERROR_IO_PENDING!=GetLastError()) ){ ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "CnnectNamePipe(%ld) error %s", GetLastError(), pipe_name); CloseHandle(hPipe); hPipe=NULL; return -1; } // WaitForSingleObject(overlapped.hEvent, 1000); CloseHandle(hEvent); return 0; }
BOOL CNeighbour::Connect(LPCTSTR lpPipeName, DWORD nMaxInstances, DWORD nTimeOut) { if ( m_hPipe != INVALID_HANDLE_VALUE ) return FALSE; SECURITY_ATTRIBUTES sa; PSECURITY_DESCRIPTOR pSD; pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH); if ( ! InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION) ) { LocalFree( (HLOCAL)pSD ); return FALSE; } if ( ! SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE) ) { LocalFree( (HLOCAL)pSD ); return FALSE; } sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = pSD; sa.bInheritHandle = TRUE; m_hPipe = CreateNamedPipe( lpPipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, nMaxInstances, TEMP_BUFFER, TEMP_BUFFER, nTimeOut, &sa ); if ( m_hPipe == INVALID_HANDLE_VALUE ) return FALSE; ZeroMemory( &m_pOverlapped, sizeof(OVERLAPPED) ); m_pOverlapped.hEvent = m_pWakeup; ConnectNamedPipe( m_hPipe, &m_pOverlapped ); TRACE("CreateNamedPipe: %s\n", lpPipeName); StartThread(); return TRUE; }
/* Return: amount of PDU data */ uint16_t MSTP_Get_Send( volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ (void) mstp_port; (void) timeout; return 0; } uint16_t MSTP_Get_Reply( volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ (void) mstp_port; (void) timeout; return 0; } static char Capture_Filename[32] = "mstp_20090123091200.cap"; static FILE *pFile = NULL; /* stream pointer */ #if defined(_WIN32) static HANDLE hPipe = INVALID_HANDLE_VALUE; /* pipe handle */ static void named_pipe_create( char *pipe_name) { fprintf(stdout, "mstpcap: Creating Named Pipe \"%s\"\n", pipe_name); /* create the pipe */ while (hPipe == INVALID_HANDLE_VALUE) { /* use CreateFile rather than CreateNamedPipe */ hPipe = CreateFile( pipe_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hPipe != INVALID_HANDLE_VALUE) { break; } /* if an error occured at handle creation */ if (!WaitNamedPipe(pipe_name, 20000)) { printf("Could not open pipe: waited for 20sec!\n" "If this message was issued before the 20sec finished,\n" "then the pipe doesn't exist!\n"); Exit_Requested = true; return; } } ConnectNamedPipe(hPipe, NULL); }
static HANDLE_OR_FILEPTR open_windows_pipe(const char *filename) { #ifdef WIN32 HANDLE pipe; int result; pipe = CreateNamedPipe(filename, PIPE_ACCESS_OUTBOUND, // write-only PIPE_TYPE_MESSAGE | PIPE_WAIT, // blocking writes 1, // only allow one pipe 70000, // write buffer. GTH max is 64k 10000, // read buffer. We don't read 0, // default timeout 0); // no security attributes if (pipe == INVALID_HANDLE_VALUE) { die("Unable to create a named pipe. Giving up."); } result = ConnectNamedPipe(pipe, 0); if (!result) { die("Unabled to connect the named pipe. Giving up."); } return pipe; #else die("Cannot open a windows named pipe on a non-windows OS. Giving up."); return 0; #endif }
static unsigned int __stdcall _ecore_con_local_win32_listening(void *data) { Ecore_Con_Server *obj = data; Ecore_Con_Server_Data *svr = eo_data_scope_get(obj, ECORE_CON_SERVER_CLASS); BOOL res; while (1) { res = ConnectNamedPipe(svr->pipe, NULL); if (!res) { ERR("Opening the connection to the client failed"); CloseHandle(svr->pipe); svr->pipe = NULL; } break; } DBG("Client connected"); printf(" ### %s\n", __FUNCTION__); _endthreadex(0); return 0; }
void WritePipe::setup_pipe() { // Create a pipe to send data _pipe = CreateNamedPipeA( _pipe_name.c_str(), // name of the pipe PIPE_ACCESS_OUTBOUND, // 1-way pipe -- send only PIPE_TYPE_BYTE | PIPE_WAIT, // send data as a byte stream 1, // only allow 1 instance of this pipe 0, // no outbound buffer 0, // no inbound buffer 10, // use default wait time NULL // use default security attributes ); if (_pipe == NULL || _pipe == INVALID_HANDLE_VALUE) { traceerr("Failed to create outbound pipe instance."); return; } bool result = ConnectNamedPipe(_pipe, NULL); if (!result) { traceerr( "Failed to make connection on named pipe."); CloseHandle(_pipe); // close the pipe return; } running = true; }
void processSpeechInput (SpeechSynthesizer *spk) { #if defined(__MINGW32__) if (speechInputHandle != INVALID_HANDLE_VALUE) { if (speechInputConnected || (speechInputConnected = ConnectNamedPipe(speechInputHandle, NULL)) || (speechInputConnected = (GetLastError() == ERROR_PIPE_CONNECTED))) { char buffer[0X1000]; DWORD count; if (ReadFile(speechInputHandle, buffer, sizeof(buffer), &count, NULL)) { if (count) sayCharacters(spk, buffer, count, 0); } else { DWORD error = GetLastError(); if (error != ERROR_NO_DATA) { speechInputConnected = 0; DisconnectNamedPipe(speechInputHandle); if (error != ERROR_BROKEN_PIPE) logWindowsSystemError("speech input FIFO read"); } } } } #elif defined(S_ISFIFO) if (speechInputDescriptor != -1) { char buffer[0X1000]; int count = read(speechInputDescriptor, buffer, sizeof(buffer)); if (count > 0) sayCharacters(spk, buffer, count, 0); } #endif /* process speech input */ }
static int monitorWindowsPipeConnect (NamedPipeObject *obj) { if (ResetEvent(obj->windows.connect.event)) { ZeroMemory(&obj->windows.connect.overlapped, sizeof(obj->windows.connect.overlapped)); obj->windows.connect.overlapped.hEvent = obj->windows.connect.event; if (ConnectNamedPipe(obj->input.descriptor, &obj->windows.connect.overlapped)) { if (doWindowsPipeConnected(obj)) { return 1; } } else { DWORD error = GetLastError(); if (error == ERROR_PIPE_CONNECTED) { if (doWindowsPipeConnected(obj)) { return 1; } } else if (error == ERROR_IO_PENDING) { if (asyncMonitorFileInput(&obj->windows.connect.monitor, obj->windows.connect.event, handleWindowsPipeConnected, obj)) { return 1; } } else { logWindowsError(error, "ConnectNamedPipe"); } } } else { logWindowsSystemError("ResetEvent"); } return 0; }
int PipeControlChannel::readNamedPipe() { BALL_LOG_SET_CATEGORY(LOG_CATEGORY); BSLS_ASSERT(INVALID_HANDLE_VALUE != d_impl.d_windows.d_handle); BALL_LOG_TRACE << "Accepting next pipe client connection" << BALL_LOG_END; if (!ConnectNamedPipe(d_impl.d_windows.d_handle, NULL)) { BALL_LOG_TRACE << "Connecting to named pipe '" << d_pipeName << "': " << describeWin32Error(GetLastError()) << BALL_LOG_END; DWORD lastError = GetLastError(); if (lastError != ERROR_PIPE_CONNECTED && lastError != ERROR_NO_DATA) { BALL_LOG_TRACE << "Failed to connect to named pipe '" << d_pipeName << "'" << BALL_LOG_END; return -1; } } while(1) { char buffer[MAX_PIPE_BUFFER_LEN]; DWORD bytesRead = 0; if (ReadFile(d_impl.d_windows.d_handle, buffer, MAX_PIPE_BUFFER_LEN, &bytesRead, NULL)) { if (bytesRead > 0) { if (buffer[bytesRead - 1] == '\n') { bytesRead--; } bslstl::StringRef stringRef(buffer, bytesRead); if (!stringRef.isEmpty()) { d_callback(stringRef); } } else { // reached EOF on a named pipe. break; } } else { BALL_LOG_TRACE << "Failed to read from named pipe '" << d_pipeName << "': " << describeWin32Error(GetLastError()) << BALL_LOG_END; break; } } DisconnectNamedPipe(d_impl.d_windows.d_handle); return 0; }
bool Connect() { if (!ConnectNamedPipe(hPipe, &Overlapped)) if (GetLastError() != ERROR_IO_PENDING) { ImDiskSvcStatus.dwWin32ExitCode = GetLastError(); delete this; return false; } HANDLE hWaitObjects[] = { Overlapped.hEvent, ImDiskSvcStopEvent }; switch (WaitForMultipleObjects(sizeof(hWaitObjects) / sizeof(*hWaitObjects), hWaitObjects, FALSE, INFINITE)) { case WAIT_OBJECT_0: { union { DWORD(CALLBACK ImDiskSvcServerSession::*Member)(); UINT(CALLBACK *Static)(LPVOID); } ThreadFunction; ThreadFunction.Member = &ImDiskSvcServerSession::Thread; KdPrint(("ImDskSvc: Creating thread.%n")); UINT id; HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, ThreadFunction.Static, this, 0, &id); if (hThread == NULL) { ImDiskSvcStatus.dwWin32ExitCode = GetLastError(); delete this; return false; } CloseHandle(hThread); ImDiskSvcStatus.dwWin32ExitCode = NO_ERROR; return true; } case WAIT_OBJECT_0 + 1: ImDiskSvcStatus.dwWin32ExitCode = NO_ERROR; delete this; return false; default: ImDiskSvcStatus.dwWin32ExitCode = GetLastError(); delete this; return false; } }