473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using Pipes in C# with Overlapped I/O

I'm trying to use Overlapped I/O from C#, and utterly failing. I've
tried to boil down my code to as simple an example as possible, in
hopes that you people can point to where I'm going wrong. I've
included my test program. The behavior I'm seeing is that the callback
routine is never called, despite the fact that the thread is (AFAIK) in
an alertable state. The program just hangs waiting for the
m_doneEvent.

In other scenarios, I'm able to transfer data, but it is sometimes
received as zeros instead of the real data. Any help, or pointers to a
good discussion of managed overlapped I/O using pipes, would be
appreciated. I'd love a website, but I can buy a book if need be.

static unsafe class Program
{
static private AutoResetEvent m_doneEvent = new AutoResetEvent(false);
static private string m_action = string.Empty;

[DllImport("kernel32.dll", EntryPoint = "ReadFileEx", SetLastError =
true,
CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern int ReadFileEx(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int nNumberOfBytesToRead,
NativeOverlapped* lpOverlapped,
[MarshalAs(UnmanagedType.FunctionPtr)] IOCompletionCallback callback
);

[DllImport("kernel32.dll", EntryPoint = "WriteFileEx", SetLastError =
true,
CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern int WriteFileEx(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int nNumberOfBytesToWrite,
NativeOverlapped* lpOverlapped,
[MarshalAs(UnmanagedType.FunctionPtr)] IOCompletionCallback callback
);

[STAThread]
static void Main()
{
ManualResetEvent actionCompletedEvent = new ManualResetEvent(false);
Overlapped overlapped = new Overlapped(0, 0,
actionCompletedEvent.SafeWaitHandle.DangerousGetHa ndle(),
null);
IOCompletionCallback callback = Finished;
byte[] bytes = new byte[1000];
NativeOverlapped* nativeOverlapped = overlapped.Pack(callback,
bytes);

IntPtr m_pipeHandle = CreateNamedPipe(
@"\\.\pipe\DSV",
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
PIPE_UNLIMITED_INSTANCES,
2048,
2048,
1000,
null
);

for(int i = 0; i < 100; i++)
{
System.Diagnostics.Trace.WriteLine("Beginning transfer");

new Random().NextBytes(bytes);
m_action = "Writing";
WriteFileEx(m_pipeHandle,
bytes,
bytes.Length,
nativeOverlapped,
callback);
m_doneEvent.WaitOne();

m_action = "Reading";
ReadFileEx(m_pipeHandle,
bytes,
1000,
nativeOverlapped,
callback);
m_doneEvent.WaitOne();
}
System.Diagnostics.Trace.WriteLine("all done");
}

[ComVisibleAttribute(true)]
static public void Finished(
uint errorCode,
uint bytesTransferred,
NativeOverlapped* overlapped)
{
System.Diagnostics.Trace.WriteLine(
m_action
+ ": errorCode=" + errorCode
+ " bytesTransferred=" + bytesTransferred);
m_doneEvent.Set();
}
}

Oct 30 '06 #1
1 8529

<dv*****@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
I'm trying to use Overlapped I/O from C#, and utterly failing. I've
tried to boil down my code to as simple an example as possible, in
hopes that you people can point to where I'm going wrong. I've
included my test program. The behavior I'm seeing is that the callback
routine is never called, despite the fact that the thread is (AFAIK) in
an alertable state. The program just hangs waiting for the
m_doneEvent.
I'm not aware of any pure .NET means for putting a thread into an alertable
wait. You end up having to p/invoke the Windows API wait functions, at
which point going to C++/CLI for "It Just Works" becomes pretty attractive.
That's what I've done.

see
http://msdn.microsoft.com/library/de..._functions.asp

I posted a bug here:
https://connect.microsoft.com/Visual...dbackID=137088
which Microsoft closed without comment, so I guess that means they aren't
going to fix it. You could validate it and post a comment to show Microsoft
that this is important to more than just one person.
>
In other scenarios, I'm able to transfer data, but it is sometimes
received as zeros instead of the real data. Any help, or pointers to a
good discussion of managed overlapped I/O using pipes, would be
appreciated. I'd love a website, but I can buy a book if need be.

static unsafe class Program
{
static private AutoResetEvent m_doneEvent = new AutoResetEvent(false);
static private string m_action = string.Empty;

[DllImport("kernel32.dll", EntryPoint = "ReadFileEx", SetLastError =
true,
CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern int ReadFileEx(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int nNumberOfBytesToRead,
NativeOverlapped* lpOverlapped,
[MarshalAs(UnmanagedType.FunctionPtr)] IOCompletionCallback callback
);

[DllImport("kernel32.dll", EntryPoint = "WriteFileEx", SetLastError =
true,
CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern int WriteFileEx(
IntPtr hFile,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
int nNumberOfBytesToWrite,
NativeOverlapped* lpOverlapped,
[MarshalAs(UnmanagedType.FunctionPtr)] IOCompletionCallback callback
);

[STAThread]
static void Main()
{
ManualResetEvent actionCompletedEvent = new ManualResetEvent(false);
Overlapped overlapped = new Overlapped(0, 0,
actionCompletedEvent.SafeWaitHandle.DangerousGetHa ndle(),
null);
IOCompletionCallback callback = Finished;
byte[] bytes = new byte[1000];
NativeOverlapped* nativeOverlapped = overlapped.Pack(callback,
bytes);

IntPtr m_pipeHandle = CreateNamedPipe(
@"\\.\pipe\DSV",
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
PIPE_UNLIMITED_INSTANCES,
2048,
2048,
1000,
null
);

for(int i = 0; i < 100; i++)
{
System.Diagnostics.Trace.WriteLine("Beginning transfer");

new Random().NextBytes(bytes);
m_action = "Writing";
WriteFileEx(m_pipeHandle,
bytes,
bytes.Length,
nativeOverlapped,
callback);
m_doneEvent.WaitOne();

m_action = "Reading";
ReadFileEx(m_pipeHandle,
bytes,
1000,
nativeOverlapped,
callback);
m_doneEvent.WaitOne();
}
System.Diagnostics.Trace.WriteLine("all done");
}

[ComVisibleAttribute(true)]
static public void Finished(
uint errorCode,
uint bytesTransferred,
NativeOverlapped* overlapped)
{
System.Diagnostics.Trace.WriteLine(
m_action
+ ": errorCode=" + errorCode
+ " bytesTransferred=" + bytesTransferred);
m_doneEvent.Set();
}
}

Oct 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lebo | last post by:
Hi List I want to effectively 'tail' the NT event log - i.e. when a new event arrives in the log, parse it and decide whether to take an action. The track I've gone down is to create an event,...
0
by: Krutibas Biswal | last post by:
Hi, I am using a script 'unbuffer' for unbuffering my outputs when using pipes. This script is based on expect and looks like this : ----------------------------------------------...
7
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h>...
6
by: Xarky | last post by:
Hi, I wrote the following program (source code below). I am using pipes, were the parent reads and the child writes. My problem is that I am writing the same line in the pipe for a fixed...
6
by: el7akika | last post by:
hi i want to communicate between simulator1 and simulator2 I use pipes, but, um, it doesn't seem to work right. The main idea is to have a process(main) simulator1 that creates...
7
by: webmaster | last post by:
Sorry if this sounds naive, but I need to know how to create a two-way pipe between my Objective-C MacOSX program and another process, like perl, for example. Is there a simple way to do this in C,...
2
by: ShawnD | last post by:
I'm having some issues when trying to read input off of a pipe using a python script. I'm trying to process packet data from tcpdump in real-time, so it's a filter that needs to read data while the...
2
by: Prathap | last post by:
Hi, I am a newbie to c++ and I am looking for examples in using pipes for IPC communication. My two processes are independent and they cannot use a unnamed pipe, so I am looking for examples that...
7
by: andrewb | last post by:
Hi all, Having some trouble using named pipes and Visual Basic .NET and would appreciate and help you could offer. Essentially I am trying to develop a simple client/server application that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.