admin 管理员组

文章数量: 887021


2023年12月18日发(作者:matlab主要工作内容)

bool CNamedPipeServer::OpenPipe(){ DWORD dwThreadId = 0; HANDLE hThread = NULL; hThread = CreateThread( NULL, // no security attribute

0, // default stack size

ConnectionThread, // thread proc (LPVOID)this, // thread parameter

0, // not suspended

&dwThreadId); // returns thread ID

if (hThread == NULL) { TRACE(TEXT("CreateThread failed, GLE=%d.n"), GetLastError()); return false; } return true;}DWORD WINAPI CNamedPipeServer::ConnectionThread(LPVOID lpvParam){ BOOL fConnected = FALSE; DWORD dwThreadId = 0; HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL; LPTSTR lpszPipename = TEXT(".pipecefnamedpipe"); // The main loop creates an instance of the named pipe and

// then waits for a client to connect to it. When the client

// connects, a thread is created to handle communications

// with that client, and this loop is free to wait for the // next client connect request. It is an infinite loop. for (;;) { TRACE(TEXT("nPipe Server: Main thread awaiting client connection on %sn"), lpszPipename); hPipe = CreateNamedPipe( lpszPipename, // pipe name

PIPE_ACCESS_DUPLEX, // read/write access

PIPE_TYPE_MESSAGE | // message type pipe

PIPE_READMODE_MESSAGE | // message-read mode

PIPE_WAIT, // blocking mode

PIPE_UNLIMITED_INSTANCES, // max. instances

BUFSIZE, // output buffer size

BUFSIZE, // input buffer size

0, // client time-out

NULL); // default security attribute

if (hPipe == INVALID_HANDLE_VALUE) { TRACE(TEXT("CreateNamedPipe failed, GLE=%d.n"), GetLastError()); return -1; } // Wait for the client to connect; if it succeeds,

// the function returns a nonzero value. If the function // returns zero, GetLastError returns ERROR_PIPE_CONNECTED.

fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); if (fConnected) { TRACE("Client connected, creating a processing thread.n");

// Create a thread for this client.

hThread = CreateThread( NULL, // no security attribute

0, // default stack size

InstanceThread, // thread proc (LPVOID)hPipe, // thread parameter

0, // not suspended

&dwThreadId); // returns thread ID

if (hThread == NULL) { TRACE(TEXT("CreateThread failed, GLE=%d.n"), GetLastError()); return -1; } else CloseHandle(hThread); } else // The client could not connect, so close the pipe.

CloseHandle(hPipe); } return 0;}DWORD WINAPI CNamedPipeServer::InstanceThread(LPVOID lpvParam)// This routine is a thread processing function to read from and reply to a client// via the open pipe connection passed from the main loop. Note this allows// the main loop to continue executing, potentially creating more threads of// of this procedure to run concurrently, depending on the number of incoming// client connections.{ HANDLE hHeap = GetProcessHeap(); TCHAR* pchRequest = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE * sizeof(TCHAR)); TCHAR* pchReply = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE * sizeof(TCHAR)); DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0; BOOL fSuccess = FALSE; HANDLE hPipe = NULL; // Do some extra error checking since the app will keep running even if this // thread fails. if (lpvParam == NULL) { TRACE("nERROR - Pipe Server Failure:n"); TRACE(" InstanceThread got an unexpected NULL value in lpvParam.n"); TRACE(" InstanceThread exitting.n"); if (pchReply != NULL) HeapFree(hHeap, 0, pchReply); if (pchRequest != NULL) HeapFree(hHeap, 0, pchRequest); return (DWORD)-1; } if (pchRequest == NULL) { TRACE("nERROR - Pipe Server Failure:n"); TRACE(" InstanceThread got an unexpected NULL heap allocation.n"); TRACE(" InstanceThread exitting.n"); if (pchReply != NULL) HeapFree(hHeap, 0, pchReply); return (DWORD)-1; } if (pchReply == NULL) { TRACE("nERROR - Pipe Server Failure:n");


本文标签: 工作 作者 内容