Click here to Skip to main content
15,898,134 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionWriteFileEx [modified] Please help Pin
noalias___6-Jan-10 6:10
noalias___6-Jan-10 6:10 
AnswerRe: WriteFileEx Pin
Richard MacCutchan6-Jan-10 6:43
mveRichard MacCutchan6-Jan-10 6:43 
GeneralRe: WriteFileEx Pin
noalias___6-Jan-10 8:05
noalias___6-Jan-10 8:05 
AnswerRe: WriteFileEx Pin
Luc Pattyn6-Jan-10 6:58
sitebuilderLuc Pattyn6-Jan-10 6:58 
GeneralRe: WriteFileEx (I have reposted with pre tags) Pin
noalias___6-Jan-10 7:05
noalias___6-Jan-10 7:05 
AnswerRe: WriteFileEx Pin
noalias___6-Jan-10 7:02
noalias___6-Jan-10 7:02 
GeneralRe: WriteFileEx Pin
Luc Pattyn6-Jan-10 7:12
sitebuilderLuc Pattyn6-Jan-10 7:12 
GeneralRe: WriteFileEx (with inditation) Pin
noalias___6-Jan-10 7:18
noalias___6-Jan-10 7:18 
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;
  //fprintf(pFile, "%s", uString);
  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;

  //
  // Preform a c++ routine on the Service.
  //

  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;
}

GeneralRe: WriteFileEx (with inditation) Pin
Richard MacCutchan6-Jan-10 7:37
mveRichard MacCutchan6-Jan-10 7:37 
GeneralRe: WriteFileEx (with inditation) Pin
noalias___6-Jan-10 7:59
noalias___6-Jan-10 7:59 
AnswerRe: WriteFileEx [modified] Please help Pin
cmk6-Jan-10 15:40
cmk6-Jan-10 15:40 
QuestionDifferentiating 2 methods with the same name for a pointer Pin
Code-o-mat6-Jan-10 0:46
Code-o-mat6-Jan-10 0:46 
AnswerRe: Differentiating 2 methods with the same name for a pointer Pin
Richard MacCutchan6-Jan-10 2:12
mveRichard MacCutchan6-Jan-10 2:12 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Code-o-mat6-Jan-10 2:15
Code-o-mat6-Jan-10 2:15 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
molesworth6-Jan-10 2:51
molesworth6-Jan-10 2:51 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Code-o-mat6-Jan-10 3:44
Code-o-mat6-Jan-10 3:44 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Richard MacCutchan6-Jan-10 3:36
mveRichard MacCutchan6-Jan-10 3:36 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Code-o-mat6-Jan-10 3:47
Code-o-mat6-Jan-10 3:47 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Richard MacCutchan6-Jan-10 3:55
mveRichard MacCutchan6-Jan-10 3:55 
AnswerRe: Differentiating 2 methods with the same name for a pointer Pin
SimonSays6-Jan-10 5:07
SimonSays6-Jan-10 5:07 
AnswerRe: Differentiating 2 methods with the same name for a pointer Pin
Moak6-Jan-10 15:30
Moak6-Jan-10 15:30 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Code-o-mat6-Jan-10 21:29
Code-o-mat6-Jan-10 21:29 
GeneralRe: Differentiating 2 methods with the same name for a pointer Pin
Moak7-Jan-10 0:32
Moak7-Jan-10 0:32 
QuestionDataBase Pin
jannathali5-Jan-10 22:38
jannathali5-Jan-10 22:38 
AnswerRe: DataBase Pin
_AnsHUMAN_ 5-Jan-10 23:09
_AnsHUMAN_ 5-Jan-10 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.