Home   Index   About
Ultimate Pack


Custom Search
Transactions on Named Pipes

A named pipe transaction is a client-server communication that combines a write operation and a read operation into a single network operation. A transaction can be used only on a duplex, message-type pipe. Transactions improve the performance of network communications between a client and a remote server. Processes can use the TransactNamedPipe and CallNamedPipe functions to perform named pipe transactions.

TransactNamedPipe is typically used by a client process to write a request message to the named pipe server and read the server's response message. The client process must specify GENERIC_READ | GENERIC_WRITE access when it opens its pipe handle by calling the CreateFile function. Then, the client process sets the pipe handle to message-read mode by calling the SetNamedPipeHandleState function. If the read buffer specified in the call to TransactNamedPipe is not large enough to hold the entire message written by the server, the function returns FALSE and GetLastError returns ERROR_MORE_DATA. The client can read the remainder of the message by calling either the ReadFile, ReadFileEx, or PeekNamedPipe function.

TransactNamedPipe is typically called by client processes, but can also be used by a server process.

The following example shows a client process using TransactNamedPipe. The example assumes that the client process has used CreateFile to connect to the pipe and SetNamedPipeHandleState to set the pipe handle's read mode, as shown in the client process example in the preceding section.

fSuccess = TransactNamedPipe(

hPipe, // pipe handle

lpszWrite, // message to server

strlen(lpszWrite)+1, // message length

chReadBuf, // buffer to receive reply

512, // size of read buffer

&cbRead, // number of bytes read

NULL); // not overlapped

// Exit if an error occurs, unless the error indicates there is more

// data in the message.

if (!fSuccess && (GetLastError() != ERROR_MORE_DATA))

MyErrExit("TransactNamedPipe");

while(1)

{

// Data from the pipe is written to STDOUT.

if (! WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),

chReadBuf, cbRead, &cbWritten, NULL) )

break;

// Break if TransactNamedPipe or ReadFile is successful.

if (fSuccess)

break;

// Read from the pipe if there is more data in the message.

fSuccess = ReadFile(

hPipe, // pipe handle

chReadBuf, // buffer to receive reply

512, // size of buffer

&cbRead, // number of bytes read

NULL); // not overlapped

// Exit if an error other than ERROR_MORE_DATA occurs.

if (! fSuccess && (GetLastError() != ERROR_MORE_DATA))

break;

}

A client process uses CallNamedPipe to combine calling the CreateFile, WaitNamedPipe (if necessary), TransactNamedPipe, and CloseHandle function into a single call. Because the pipe handle is closed before the function returns, any additional bytes in the message are lost if the message is larger than the specified size of the read buffer. The following example shows the use of CallNamedPipe.

// Combines connect, wait, write, read, and close operations.

fSuccess = CallNamedPipe(

lpszPipename, // pipe name

lpszWrite, // message to server

strlen(lpszWrite)+1, // message length

chReadBuf, // buffer to receive reply

512, // size of read buffer

&cbRead, // number of bytes read

20000); // waits for 20 seconds

if (fSuccess || GetLastError() == ERROR_MORE_DATA)

{

// Data from the pipe is written to STDOUT.

WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),

chReadBuf, cbRead, &cbWritten, NULL);

// The pipe is closed, so no more bytes can be read from the

// message.

if (! fSuccess)

printf("\n...extra data in message was lost\n");

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun