summaryrefslogtreecommitdiffstats
path: root/Kod/display/ClientServerApp/Common/WriteABuffer.cpp
blob: 41ecf52277432570518000cbf28032c207ec7775 (plain) (blame)
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
67


#include "WriteABuffer.h"


BOOL WriteABuffer(wchar_t* lpBuf, HANDLE hComm)
{
	OVERLAPPED osWrite = {0};
	DWORD dwWritten;
	BOOL fRes;

	unsigned char printBuf[COMSTR_LEN];
	unsigned char* ptrPrintBuf;
	ptrPrintBuf = &printBuf[0];

	int lenBuf = (int) lpBuf[1];
	int ii;

//	printf("\tInteger: %d\n",lenBuf);

//	printf("\tLength of lpBuf: %d\n",wcslen(lpBuf));

	for (ii=0; ii<lenBuf+EXTRA_LEN; ii++) 
	{
		printBuf[ii] = (unsigned char) lpBuf[ii];
//		printf("\nWriteABuffer, variable printBuf[%d]: %d \n", ii, printBuf[ii]); 
	}
	printBuf[ii] ='\0';
//    printf("\nWriteABuffer, variable printBuf[%d]: %d \n", ii, printBuf[ii]); 






	// Create this writes OVERLAPPED structure hEvent.
	osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (osWrite.hEvent == NULL)
		// Error creating overlapped event handle.
		return FALSE;

	// Issue write.
//	printf("\tNum to write: %i\n",dwToWrite);
//	if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
	if (!WriteFile(hComm, ptrPrintBuf, lenBuf+3, &dwWritten, &osWrite)) {
		if (GetLastError() != ERROR_IO_PENDING) { 
			// WriteFile failed, but it isn't delayed. Report error and abort.
			fRes = FALSE;
		}
		else {
			// Write is pending.
			if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
				fRes = FALSE;
			else
				// Write operation completed successfully.
				fRes = TRUE;
		}
	}
	else
		// WriteFile completed immediately.
		fRes = TRUE;

	CloseHandle(osWrite.hEvent);
	return fRes;
}