Sie sind auf Seite 1von 4

Find it myself Ask the community Get live help

Select the product you need help with

Windows

Internet Explorer

Office

Surface

Xbox

Skype

Windows Phone

More products

How to use named pipes for interprocess communication in Visual Basic .NET or in Visual Basic 2005
Article ID: 871044 - View products that this article applies to.

SUMMARY
This step-by-step article discusses how to use named pipes in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005 for interprocess communication. This article includes a code sample that demonstrates client/server communication by using a named pipe in Visual Basic .NET or in Visual Basic 2005. The article describes the interprocess communication by creating a named pipe server and a named pipe client. The communication over the pipe is performed in the following way: Create a named pipe. Block the server application by using the ConnectNamedPipe function until a client connects. Connect to the server by using the CallNamedPipe function. Call the ReadFile function or the WriteFile function to communicate over the pipe. Call the DisconnectNamedPipe function when the process finishes using the pipe. Call the CloseHandle function on the named pipe after you completed communicating over the pipe.

INTRODUCTION
A named pipe is a one-way or duplex pipe for communication between a pipe server and one or more pipe clients. You can user named pipes to provide communication between processes on the same computer or between processes on different computers across a network. The term "named pipe server" refers to a process that creates a named pipe, and the term "named pipe client" refers to a process that connects to an instance of a named pipe. You can use Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to create applications that communicate to other processes by using named pipes. This article contains a code sample that uses a named pipe to communicate between two Visual Basic .NET or Visual Basic 2005 Windows applications.

Requirements
This article assumes that you are familiar with the following topics: Windows applications Visual Basic .NET programming Using pipes The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need: Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 Microsoft .NET Framework

Create a named pipe server


To create a named pipe server, and then communicate with the client over the pipe, follow these steps: 1. 2. 3. 4. 5. Create a named pipe. Call the ConnectNamedPipe function to block the server until a client connects. Call the ReadFile function or the WriteFile function to communicate over the pipe. Call the DisconnectNamedPipe function when the process finishes using the pipe. Call the CloseHandle function on the named pipe.

Design a Windows application that creates a named pipe server


To design a Windows application that creates a named pipe server by using Visual Basic .NET or Visual Basic 2005, follow these steps:

1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. 2. On the File menu, point to New, and then click Project. 3. Under Project Types, click Visual Basic Projects, and then click Windows Application under Templates. Note In Visual Studio 2005, click Visual Basic under Project Types. In the Name box, type MyServerApp, and then click OK. By default, a form that is named Form1 is created. Add three Button controls to the Form1 form. On the View menu, click Properties Window. Set the Text property of the Button controls to the following values: Name Text

4. 5. 6. 7.

Button1 Create a named pipe Button2 Wait for the client connections Button3 Disconnect the server 8. Add a Label control to the Form1 form. The Label1 Label control is added to the Form1 form.

Add all the declarations to a module in the Windows application


To add the function declarations that are required to implement the interprocess communication by using the named pipe, follow these steps: 1. In Solution Explorer, right-click MyServerApp, point to Add, and then click Add Module. 2. In the Add New Item - MyServerApp dialog box, click Open. 3. Add the following code to the Module1.vb module: P u b l i cC o n s tF I L E _ A T T R I B U T E _ N O R M A LA sS h o r t=& H 8 0 SP u b l i cC o n s tF I L E _ F L A G _ N O _ B U F F E R I N GA sI n t e g e r=& H 2 0 0 0 0 0 0 0P u b l i c C o n s tF I L E _ F L A G _ W R I T E _ T H R O U G HA sI n t e g e r=& H 8 0 0 0 0 0 0 0P u b l i cC o n s tP I P E _ A C C E S S _ D U P L E XA sS h o r t=& H 3 SP u b l i cC o n s t P I P E _ R E A D M O D E _ M E S S A G EA sS h o r t=& H 2 SP u b l i cC o n s tP I P E _ T Y P E _ M E S S A G EA sS h o r t=& H 4 SP u b l i cC o n s tP I P E _ W A I TA sS h o r t= & H 0 SP u b l i cC o n s tI N V A L I D _ H A N D L E _ V A L U EA sS h o r t=1D e c l a r eF u n c t i o nC r e a t e N a m e d P i p eL i b" k e r n e l 3 2 "A l i a s " C r e a t e N a m e d P i p e A "_( B y V a ll p N a m eA sS t r i n g ,B y V a ld w O p e n M o d eA sI n t e g e r ,_B y V a ld w P i p e M o d eA sI n t e g e r ,B y V a l n M a x I n s t a n c e sA sI n t e g e r ,_B y V a ln O u t B u f f e r S i z eA sI n t e g e r ,B y V a ln I n B u f f e r S i z eA sI n t e g e r ,_B y V a ln D e f a u l t T i m e O u tA s I n t e g e r ,B y V a ll p S e c u r i t y A t t r i b u t e sA sI n t P t r_)A sI n t e g e rD e c l a r eF u n c t i o nC o n n e c t N a m e d P i p eL i b" k e r n e l 3 2 "_( B y V a l h N a m e d P i p eA sI n t e g e r ,B y V a ll p O v e r l a p p e dA sI n t e g e r )A sI n t e g e rD e c l a r eF u n c t i o nD i s c o n n e c t N a m e d P i p eL i b" k e r n e l 3 2 "_ ( B y V a lh N a m e d P i p eA sI n t e g e r )A sI n t e g e rD e c l a r eF u n c t i o nW r i t e F i l eL i b" k e r n e l 3 2 "_( B y V a lh F i l eA sI n t e g e r ,B y R e f l p B u f f e r ( )A sB y t e ,_B y V a ln N u m b e r O f B y t e s T o W r i t eA sI n t e g e r ,B y R e fl p N u m b e r O f B y t e s W r i t t e nA sI n t e g e r ,_B y V a l l p O v e r l a p p e dA sI n t e g e r_)A sI n t e g e rD e c l a r eF u n c t i o nR e a d F i l eL i b" k e r n e l 3 2 "_( B y V a lh F i l eA sI n t e g e r ,B y R e fl p B u f f e r A sI n t e g e r ,_B y V a ln N u m b e r O f B y t e s T o R e a dA sI n t e g e r ,B y R e fl p N u m b e r O f B y t e s R e a dA sI n t e g e r ,_B y V a ll p O v e r l a p p e dA sI n t e g e r _)A sI n t e g e rD e c l a r eF u n c t i o nF l u s h F i l e B u f f e r sL i b" k e r n e l 3 2 "_( B y V a lh F i l eA sI n t e g e r )A sI n t e g e rD e c l a r eF u n c t i o n C l o s e H a n d l eL i b" k e r n e l 3 2 "_( B y V a lh O b j e c tA sI n t e g e r )A sI n t e g e r

Write code to create a named pipe server


After you create a named pipe, wait for the connections from the client. When the client connects to the server, read or write the data over the pipe. To do this, follow these steps: 1. In Solution Explorer, right-click Form1, and then click Open. 2. In Design view of the Form1 form, double-click Create a named pipe, and then add the following code to the Button1_Click procedure: D i mo p e n M o d e ,p i p e M o d eA sI n t e g e r' C r e a t et h en a m e dp i p eo p e n M o d e=P I P E _ A C C E S S _ D U P L E XO rF I L E _ F L A G _ W R I T E _ T H R O U G Hp i p e M o d e =P I P E _ W A I TO rP I P E _ T Y P E _ M E S S A G EO rP I P E _ R E A D M O D E _ M E S S A G Eh P i p e=C r e a t e N a m e d P i p e ( p i p e N a m e ,o p e n M o d e ,p i p e M o d e ,1 0 ,1 0 0 0 0 , 2 0 0 0 ,1 0 0 0 0 ,I n t P t r . Z e r o )L a b e l 1 . T e x t=" C r e a t e dt h en a m e dp i p ea n dw a i t i n gf o rt h ec l i e n t s . "B u t t o n 1 . V i s i b l e=F a l s e B u t t o n 2 . V i s i b l e=T r u eB u t t o n 3 . V i s i b l e=T r u e 3. Add the following code before the Button1_Click procedure: P r i v a t eC o n s tp i p e N a m eA sS t r i n g=" \ \ . \ p i p e \ M y P i p e "P r i v a t eC o n s tB U F F S I Z EA sS h o r t=1 0 0 0 0P r i v a t eB u f f e r ( B U F F S I Z E )A s B y t eP r i v a t eh P i p eA sI n t e g e r 4. In Solution Explorer, double-click Form1.vb. 5. In Design view of the Form1 form, double-click Wait for the client connections, and then add the following code to the Button2_Click procedure: D i mb y t e C o u n t ,i ,r e s ,c b n C o u n tA sI n t e g e rF o ri=0T oB U F F S I Z E-1' F i l la na r r a yo fn u m b e r sB u f f e r ( i )=iM o d2 5 6N e x t i' W a i tf o rac o n n e c t i o n ,b l o c ku n t i lac l i e n tc o n n e c t sL a b e l 1 . T e x t=" W a i t i n gf o rc l i e n tc o n n e c t i o n s "M e . R e f r e s h ( )D or e s =C o n n e c t N a m e d P i p e ( h P i p e ,0 )' R e a dt h ed a t as e n tb yt h ec l i e n to v e rt h ep i p ec b n C o u n t=4r e s=R e a d F i l e ( h P i p e ,b y t e C o u n t , L e n ( b y t e C o u n t ) ,c b n C o u n t ,0 )I fb y t e C o u n t>B U F F S I Z ET h e n' C l i e n tr e q u e s t e df o rb y t e C o u n tb y t e sb y t e C o u n t=B U F F S I Z E' b u t o n l ys e n du pt o2 0 0 0 0b y t e sE n dI f' W r i t et h en u m b e ro fb y t e sr e q u e s t e db yt h ec l i e n tr e s=W r i t e F i l e ( h P i p e ,B u f f e r , b y t e C o u n t ,c b n C o u n t ,0 )r e s=F l u s h F i l e B u f f e r s ( h P i p e )' D i s c o n n e c tt h en a m e dp i p e .r e s=D i s c o n n e c t N a m e d P i p e ( h P i p e )' L o o p u n t i lt h ec l i e n tm a k e sn om o r er e q u e s t sf o rd a t a .L o o pU n t i lb y t e C o u n t=0L a b e l 1 . T e x t=" R e a do rW r i t ec o m p l e t e d " B u t t o n 2 . V i s i b l e=F a l s e 6. In Design view, double-click Form1, and then add the following code to the Form1_Load procedure: B u t t o n 2 . V i s i b l e=F a l s eB u t t o n 3 . V i s i b l e=F a l s e 7. In Solution Explorer, double-click Form1.vb. 8. In Design view of the Form1 form, double-click Disconnect the server, and then add the following code to the Button3_Click procedure: D i mr e sA sI n t e g e r' C l o s et h ep i p eh a n d l ew h e nt h ec l i e n tm a k e sn or e q u e s t sC l o s e H a n d l e ( h P i p e )L a b e l 1 . T e x t=" D i s c o n n e c t e d

t h en a m e dp i p e " 9. On the Build menu, click Build Solution.

Create a named pipe client


To create a named pipe client that communicates with the server, follow these steps: 1. Call the CreateFile function to create a handle to the named pipe. 2. Call the ReadFile function or the WriteFile function to communicate over the pipe. 3. Call the CloseHandle function on the handle that is created in the CreateFile function. You can also use a named pipe transaction for client/server communication. The named pipe transaction combines a write operation and a read operation in a single network operation. A transaction can be used only on a duplex, message-type pipe. Processes can use the TransactNamedPipe function or the CallNamedPipe function to perform the named pipe transactions. In this code sample, you use the CallNamedPipe function to connect to the named pipe server, write the data to the pipe, and then read the data from the pipe.

Design a Windows application that communicates with the named pipe server
To design a Visual Basic .NET or Visual Basic 2005 Windows application that is used to connect to the named pipe server, follow these steps: 1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. 2. On the File menu, point to New, and then click Project. 3. Under Project Types, click Visual Basic Projects, and then click Windows Application under Templates. Note In Visual Studio 2005, click Visual Basic under Project Types. In the Name box, type MyClientApp, and then click OK. By default, a form that is named Form1 is created. Add a Button control to the Form1 form. Right-click Button1, and then click Properties. Set the Text property to Connect to the server. Add a Label control to the Form1 form. The Label1 Label control is added to Form1 form. Set the Visible property of Label1 Label control to False. Add two TextBox controls to the Form1 form.

4. 5. 6. 7. 8. 9. 10.

Write code to connect to the named pipe server


Connect to the named pipe server by using the CallNamedPipe function. After connecting to the server, the CallNamedPipe function writes to the pipe, reads from the pipe, and then closes the pipe. To connect to the server, and then read or write the data, follow these steps: 1. In Design view, double-click Connect to the server, and then add the following code to the Button1_Click procedure: D i mi ,r e s ,c b R e a d , n u m B y t e sA sI n t e g e rD i mb A r r a y ( )A sB y t eD i mt e m pA sS t r i n gn u m B y t e s=C I n t ( T e x t B o x 1 . T e x t )I fn u m B y t e s <0T h e nM e s s a g e B o x . S h o w ( " V a l u em u s tb ea tl e a s t0 . " ,M s g B o x S t y l e . O K O n l y )E x i tS u bE n dI fI fn u m B y t e s=0T h e n L a b e l 1 . V i s i b l e=T r u eL a b e l 1 . T e x t=" T h ec o n n e c t i o nt ot h es e r v e ri sd i s c o n n e c t e d . "B u t t o n 1 . V i s i b l e=F a l s e T e x t B o x 1 . V i s i b l e=F a l s eT e x t B o x 2 . V i s i b l e=F a l s eE n dI fI fn u m B y t e s>B U F F S I Z ET h e nn u m B y t e s=B U F F S I Z EE n dI fR e D i m b A r r a y ( n u m B y t e s )' C r e a t et h er e t u r nb u f f e r' C a l lt h eC a l l N a m e d P i p ef u n c t i o nt od ot h et r a n s a c t i o n sr e s= C a l l N a m e d P i p e ( p i p e N a m e ,n u m B y t e s ,L e n ( n u m B y t e s ) ,b A r r a y ( 0 ) ,n u m B y t e s ,c b R e a d ,3 0 0 0 0 )' W a i tu pt o3 0s e c o n d sf o rar e s p o n s e ' F o r m a tt h ed a t ar e c e i v e d ,a n dt h e nd i s p l a yt h ed a t ai nt h et e x tb o xI fr e s>0T h e nt e m p=F o r m a t ( b A r r a y ( 0 ) ,"0 0 0 " )F o r i=1T oc b R e a d-1I f( iM o d1 6 )=0T h e nt e m p=t e m p&v b C r L ft e m p=t e m p&""&F o r m a t ( b A r r a y ( i ) ," 0 0 0 " )N e x ti T e x t B o x 2 . T e x t=t e m pE l s eM e s s a g e B o x . S h o w ( " E r r o rn u m b e r"&E r r . L a s t D l l E r r o r&_" w h i l et r y i n gt oc a l lt h eC a l l N a m e d P i p e f u n c t i o n . " ,M s g B o x S t y l e . O K O n l y )E n dI f 2. Add the following code before the Button1_Click procedure: P r i v a t eC o n s tp i p e N a m eA sS t r i n g=" \ \ . \ p i p e \ M y P i p e "P r i v a t eC o n s tB U F F S I Z EA sI n t e g e r=1 0 0 0 0P r i v a t eh p i p eA sI n t e g e r P u b l i cC o n s tI N V A L I D _ H A N D L E _ V A L U EA sS h o r t=1P u b l i cD e c l a r eF u n c t i o nC a l l N a m e d P i p eL i b" k e r n e l 3 2 "A l i a s " C a l l N a m e d P i p e A "_( B y V a ll p N a m e d P i p e N a m eA sS t r i n g ,_B y R e fl p I n B u f f e rA sI n t e g e r ,_B y V a ln I n B u f f e r S i z eA sI n t e g e r ,_ B y R e fl p O u t B u f f e rA sB y t e ,_B y V a ln O u t B u f f e r S i z eA sI n t e g e r ,_B y R e fl p B y t e s R e a dA sI n t e g e r ,B y V a ln T i m e O u tA sI n t e g e r ) A sI n t e g e r 3. On the Build menu, click Build Solution.

Verify that the code works


To verify that your code works, follow these steps: 1. To start the server application, click Start on the Debug menu of the MyServerApp project. 2. On the Form1 form, click Create a named pipe, and then click Wait for the client connections. The application is now blocked and waits for the client to connect. 3. To start the client application, click Start on the Debug menu of the MyClientApp project. 4. On the Form1 form, type 10 in the TextBox1 box, and then click Connect to the server. You can see the byte array that is received in the TextBox2 box.

5. To disconnect the client application from the server, type 0 in the TextBox1 box in the client application, and then click Connect to the server. 6. Close the client application. 7. To disconnect the server end of the named pipe, and then close the server application, click Disconnect the server on the Form1 form of the server application.

REFERENCES
For more information, visit the following Microsoft Developer Network (MSDN) Web sites: Named pipes http://msdn2.microsoft.com/en-us/library/aa365590.aspx (http://msdn2.microsoft.com/en-us/library/aa365590.aspx) Transactions on named pipes http://msdn2.microsoft.com/en-us/library/aa365789.aspx (http://msdn2.microsoft.com/en-us/library/aa365789.aspx) Pipe functions http://msdn2.microsoft.com/en-us/library/aa365781.aspx (http://msdn2.microsoft.com/en-us/library/aa365781.aspx)

Properties
Article ID: 871044 - Last Review: January 11, 2007 - Revision: 2.4 APPLIE S TO
Microsoft Visual Basic 2005 Microsoft Visual Basic .NET 2003 Standard Edition Microsoft Visual Basic .NET 2002 Standard Edition

K eywo r d s : kbvs2005swept kbvs2005applies kbserver kbpipes kbipc kbclient kbhowtomaster kbhowto KB871044

Give Feedback
Back to the top

Das könnte Ihnen auch gefallen