No I don't and I've had a look at the code too and no I can't see a problem with it. It compiles fine, the connect and disconnect code is ok. It is only when I un-rem the receive code that things majorly play up and both programs freeze. I'm stuck, really I am.
Server Code.
#pragma warning(disable:4995)
#pragma comment(lib, "shell32.lib")
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <strsafe.h>
#include <time.h>
#include "psapi.h"
#define FNAME_LEN 1000
#define PNAME_LEN 1000
#define SEND_LEN 1024
#define RECV_LEN 64
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
} PIPEINST, *PPIPEINST;
typedef struct _THREAD_CONTEXT
{
HANDLE hPort;
HANDLE hCompletion;
DWORD uTemporary;
} THREAD_CONTEXT, *PTHREAD_CONTEXT;
HANDLE NewPipe(LPWSTR PipeName, LPOVERLAPPED lpOverlapped)
{
HANDLE hPipe;
hPipe=CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, PNAME_LEN*sizeof(TCHAR), PNAME_LEN*sizeof(TCHAR), 1, NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
printf("CreateNamedPipe failed.\n");
return NULL;
}
if((ConnectNamedPipe(hPipe, lpOverlapped)))
{
printf("ConnectNamedPipe failed.\n");
return NULL;
}
return hPipe;
}
VOID WINAPI CompletedWrite(DWORD dwErr, DWORD dwWritten, LPOVERLAPPED lpOverLap)
{
printf("Written %d.\n", dwWritten);
}
BOOLEAN SendToGUI(HANDLE hPipe, PPIPEINST pPipeInst, DWORD numb, PWCHAR str)
{
PWCHAR buffer;
buffer = (PWCHAR) malloc (SEND_LEN);
swprintf(buffer, L"%d\t%ws\0\0", numb, str);
printf("Sending.. %ws\n", buffer);
StringCchCopy(pPipeInst->tzSend, wcslen(buffer)+2, buffer);
pPipeInst->dwSendLength = (wcslen(pPipeInst->tzSend) * 2);
if(WriteFileEx(hPipe, pPipeInst->tzSend, pPipeInst->dwSendLength + 2, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedWrite))
{
free(buffer);
return TRUE;
}
else
{
printf("WriteFileEx failed '%d'.\n", GetLastError());
free(buffer);
return FALSE;
}
}
VOID WINAPI CompletedRead(DWORD dwErr, DWORD dwRead, LPOVERLAPPED lpOverLap)
{
printf("Read %d bytes.\n", dwRead);
}
DWORD ReceiveFromGUI(HANDLE hPipe, PPIPEINST pPipeInst)
{
DWORD BytesRead = 0;
pPipeInst->dwRecvLength = 0;
if(ReadFileEx(hPipe, pPipeInst->tzRecv, RECV_LEN, (LPOVERLAPPED) pPipeInst, (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedRead))
{
return TRUE;
}
else
{
printf("ReadFileEx failed.\n");
return FALSE;
}
}
DWORD Thread(PTHREAD_CONTEXT pThreadContext)
{
OVERLAPPED hEventOverlapped;
PWCHAR PipeName;
HANDLE hPipe;
HANDLE hEvent;
PPIPEINST pPipeInst;
BOOLEAN IsConnected;
if((hEvent=CreateEvent(NULL, TRUE, TRUE, NULL)) == NULL)
{
printf("CreateEvent failed.\n");
goto end_thread;
}
else
{
hEventOverlapped.hEvent = hEvent;
}
if((PipeName=(PWCHAR) malloc(PNAME_LEN+2)) == NULL)
{
printf("malloc(PipeName) failed.\n");
goto end_thread;
}
swprintf(PipeName, L"\\\\.\\pipe\\PipeName%d\0", pThreadContext->uTemporary);
printf("PipeName %ws.\n", PipeName);
pThreadContext->uTemporary = 1;
IsConnected = FALSE;
if((pPipeInst = (PPIPEINST) GlobalAlloc(GPTR, sizeof(PIPEINST)+2)) !=NULL)
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) !=NULL)
while(TRUE)
{
if(!IsConnected)
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Client has connected.\n");
IsConnected = TRUE;
}
ConnectNamedPipe(hPipe, &hEventOverlapped);
if(GetLastError() != ERROR_PIPE_CONNECTED || GetLastError() == ERROR_PIPE_LISTENING)
{
printf("Client has disconnected.\n");
IsConnected = FALSE;
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
if((hPipe = NewPipe (PipeName, &hEventOverlapped)) == NULL)
{
printf("Unable to recreate new pipe.\n");
break;
}
}
else
{
if((SendToGUI(hPipe, pPipeInst, 1, L"string")) == FALSE)
{
printf("SendToGUI failed '%d'\n", GetLastError());
}
else
{
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
ReceiveFromGUI(hPipe, pPipeInst);
WaitForSingleObjectEx(hEvent, INFINITE, TRUE);
printf("Retrieved %c\n", pPipeInst->tzRecv[0]);
}
}
}
end_thread:
printf("%ws pipe exiting..\n", PipeName);
if(PipeName == NULL); else free(PipeName);
if(hPipe == NULL); else CloseHandle(hPipe);
if(pPipeInst == NULL); else free(pPipeInst);
return 1;
}
void __cdecl main()
{
HANDLE hThread[1];
THREAD_CONTEXT ThreadContext;
DWORD dwThreadId;
DWORD dwi;
for (dwi=0; dwi<1; dwi++)
{
ThreadContext.uTemporary = dwi;
hThread[dwi] = CreateThread(NULL, 0, Thread, &ThreadContext, 0, &dwThreadId);
if(hThread[dwi] == NULL)
{
printf("CreateThread failed.\n");
return;
}
while(ThreadContext.uTemporary == dwi);
}
WaitForMultipleObjectsEx(dwi, hThread, TRUE, INFINITE, FALSE);
printf("Exit success.\n");
return;
}
Client Code
#pragma warning(disable:4995)
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include <string.h>
#include <crtdbg.h>
#include <assert.h>
#include <fltuser.h>
#include <wchar.h>
#include <strsafe.h>
#include <conio.h>
#define RECV_LEN 1000
#define SEND_LEN 1000
#define PNAME_LEN 1000
int OpenPipe(DWORD PipeNumber, PCHAR PipeName);
int ReadPipe(DWORD PipeNumber, PCHAR PipeRead, DWORD ReadLength);
int WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength);
int ClosePipe(DWORD PipeNumber);
typedef struct _PIPEINST
{
OVERLAPPED Overlapped;
HANDLE hPipe;
TCHAR tzSend[SEND_LEN];
DWORD dwSendLength;
TCHAR tzRecv[RECV_LEN];
DWORD dwRecvLength;
DWORD dwState;
BOOL fPendingIO;
} PIPEINST, *PPIPEINST;
PPIPEINST pPipeInst[1];
HANDLE hEvents[1];
int WriteToLog(char* uString)
{
FILE* pFile;
if((pFile = fopen("logfile.log", "a+")) == NULL) return -1;
printf("%s\n", uString);
fclose(pFile);
return 0;
}
int __stdcall OpenPipe(DWORD PipeNumber, PCHAR PipeName)
{
DWORD dwMode;
BOOL fSuccess = FALSE;
WCHAR wBuffer[PNAME_LEN];
UCHAR ErrorMessage[1024];
pPipeInst[PipeNumber] = (PPIPEINST) malloc (sizeof(PIPEINST));
MultiByteToWideChar(CP_ACP, 0, PipeName, -1, wBuffer, strlen(PipeName));
wBuffer[strlen(PipeName)] = '\0';
hEvents[PipeNumber] = CreateEvent(NULL, TRUE, TRUE, NULL);
if(hEvents[PipeNumber] == NULL)
{
printf("CreateEvent failed.\n");
return 1;
}
pPipeInst[PipeNumber]->Overlapped.hEvent = hEvents[PipeNumber];
while (1)
{
pPipeInst[PipeNumber]->hPipe = CreateFile(wBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (pPipeInst[PipeNumber]->hPipe != INVALID_HANDLE_VALUE) break;
if (GetLastError() != ERROR_PIPE_BUSY) return 1;
if (!WaitNamedPipe(wBuffer, 20000)) return 1;
}
dwMode = PIPE_TYPE_BYTE;
fSuccess = SetNamedPipeHandleState(pPipeInst[PipeNumber]->hPipe, &dwMode, NULL, NULL);
if (!fSuccess) return 1;
return 0;
}
int __stdcall ReadPipe(DWORD PipeNumber, PCHAR pPipeRead, DWORD ReadLength)
{
PWCHAR PipeRead;
BOOL fSuccess = FALSE;
PipeRead = (PWCHAR) malloc (RECV_LEN);
if(PipeRead == NULL)
{
printf("malloc(PipeRead) failed.\n");
return 0;
}
ReadFile(pPipeInst[PipeNumber]->hPipe, PipeRead, RECV_LEN, NULL, &pPipeInst[PipeNumber]->Overlapped);
fSuccess = GetOverlappedResult(pPipeInst[PipeNumber]->hPipe, &pPipeInst[PipeNumber]->Overlapped, &ReadLength, FALSE);
if(fSuccess)
{
sprintf(pPipeRead, "%ws\0", PipeRead);
free(PipeRead);
return ReadLength;
}
free(PipeRead);
return 0;
}
int __stdcall WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength)
{
BOOL fSuccess = FALSE;
PWCHAR wBuffer;
DWORD cbWritten;
wBuffer = (PWCHAR) malloc (SEND_LEN);
if(wBuffer == NULL)
{
printf("malloc(wBuffer) failed.\n");
return 1;
}
MultiByteToWideChar(CP_ACP, 0, PipeWrite, -1, wBuffer, strlen(PipeWrite));
wBuffer[strlen(PipeWrite)] = '\0';
fSuccess = WriteFile(pPipeInst[PipeNumber]->hPipe, wBuffer, wcslen(wBuffer) * 2, &cbWritten, NULL);
if (!fSuccess)
{
free(wBuffer);
return 1;
}
else
{
free(wBuffer);
return 0;
}
}
int __stdcall ClosePipe(DWORD PipeNumber)
{
CloseHandle(pPipeInst[PipeNumber]->hPipe);
return 0;
}
void __cdecl main()
{
PCHAR Pipe;
DWORD n = 0;
DWORD ReadLength = 0;
Pipe = (PCHAR) malloc (1024);
if(OpenPipe(0, "\\\\.\\Pipe\\PipeName0") != 0) return;
printf ("Connected.\n");
while (TRUE)
{
if((ReadLength = ReadPipe(n, Pipe, 0)) > 0)
{
printf("Read (%i bytes):\n %s\n", ReadLength, Pipe);
printf("Sending..\n");
printf("Responce: %i (1 is failure.)\n", WritePipe(n, "a", 1));
}
}
return;
}
|