Try OpenEdge Now
skip to main content
Programming Interfaces
External Program Interfaces : Named Pipes : Windows named pipes : Coding the C program
 
Coding the C program
The non-OpenEdge program creates, connects, reads, writes, and closes the Windows named pipe or pipes.
The i-cpipe.c C program demonstrates creating, connecting, reading, writing, and closing Windows named pipe custpipe.
i-cpipe.c
/* C program that reads and writes a Windows NT named pipe */
/* 1. Declare include files */
#include <windows.h>
#include <stdio.h>
#include <wincon.h>
#include <winerror.h>
#include <conio.h>
void main()
{
/* 2. Define automatic data items */
HANDLE hPipe;
char buffer[4096];
DWORD dwBytesRead;
BOOL bRet;
int iPipeType;
int iLen;
/* 3. Make window title meaningful */
/* 4. Create the named pipe */
printf("Creating Named Pipe custpipe\n");
hPipe = CreateNamedPipe("\\\\.\\pipe\\custpipe",
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1,
0,
0,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
printf("Error creating pipe, %ld\n", GetLastError());
exit(GetLastError());
}
printf("custpipe created successfully\n");
/* 5. Solicit user input */
do
{ printf("\nPress 1 for Read, 2 for Write: ");
iPipeType = getch(); }
while (iPipeType != '1' && iPipeType != '2');
/* 6. Connect the named pipe */
printf("\nWaiting for connection...");
if (ConnectNamedPipe(hPipe, NULL) == FALSE)
{
printf("Error connecting to named pipe, %ld", GetLastError());
exit(GetLastError());
}
printf("and connection established\n");
/* 7. Read and write the pipe */
switch(iPipeType)
{
case '1': /* read */
while (TRUE)
{
bRet = ReadFile(hPipe, &buffer, 4096, &dwBytesRead, NULL);
if (bRet == FALSE)
{
/* if the pipe went away, the ABL app did an OUTPUT CLOSE */
if (GetLastError() == ERROR_BROKEN_PIPE)
printf("Error reading from pipe, %ld\n", GetLastError());
break;
}
printf("Data read = %ld %s\n", dwBytesRead, buffer);
} /* end while */
break;
/*} end switch */
case '2': /* write */
while (TRUE)
{
printf(
"Enter data to send, followed by ENTER: (Blank to exit) \n"
);
gets(buffer);
if (buffer[0] == 0)
break;
iLen = strlen(buffer);
buffer[iLen++] = 0x0d; /* CR */
buffer[iLen++] = 0x0a; /* LF */
bRet = WriteFile(hPipe, &buffer, (DWORD) iLen, &dwBytesRead,
NULL);
if (bRet)
{ FlushFileBuffers(hPipe);
printf("%ld bytes flushed to pipe\n", dwBytesRead);
}
else
printf("Error writing to pipe: %d\n", GetLastError());
} /* end while */
break;
} /* end switch */
/* 8. Close and exit */
CloseHandle(hPipe);
printf("custpipe closed successfully\nBye");
}
The i-cpipe.c program:
1. Declares include files.
2. Defines data items.
3. Calls an NT Console API function to give the window a more descriptive title.
4. Creates named pipe custpipe. The PIPE_ACCESS_DUPLEX flag makes the pipe read/write. The PIPE_WAIT flag makes the pipe synchronous. The program checks for errors and prints debugging messages here and throughout.
5. Solicits and accepts a value ("1" to read the pipe, "2" to write the pipe) from the user.
6. Connects the named pipe. This makes the pipe available to other applications, processes and threads.
7. Either reads the pipe and displays the number of bytes read along with the actual data, or else solicits a customer number, appends a carriage return and a line feed (CR/LF), writes the result to the named pipe, and flushes the buffers.
The program uses the same API calls for named pipes as for files. Named pipes are an integral part of the Windows file system, just as they are an integral part of UNIX file systems.
If the program did not append a CR/LF to the data it writes to the named pipe, the ABL program's SET input statement would wait for a line terminator or EOF marker, which does not ordinarily appear until the pipe is closed.
The two programs use a quick and dirty hack to signal "named pipe EOF." The ABL program closes the named pipe with the OUTPUT CLOSE statement, which causes the C program's ReadFile() call to raise a BROKEN_PIPE error, which the C program interprets as "named pipe EOF."
8. Closes the named pipe and exits.