问题
We developed an XP application that uses ConnectNamedPipe() in blocking mode.
When testing on Win 7, the application behaves as if it is unblocked: ConnectNamedPipe() returns before its VBS client connects. An exception is raised ("Waiting for a process to open the other end of pipe") when calling StreamReader.ReadLine(). When running with the debugger, this does not occur even in Win 7!
These are the functions that we are using: [
DllImport("kernel32.dll", SetLastError = true)]
public static extern SafeFileHandle CreateNamedPipe(
String pipeName,
uint dwOpenMode,
uint dwPipeMode,
uint nMaxInstances,
uint nOutBufferSize,
uint nInBufferSize,
uint nDefaultTimeOut,
IntPtr lpSecurityAttributes);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int ConnectNamedPipe(
SafeFileHandle hNamedPipe,
IntPtr lpOverlapped);
After we wrote this, we found an MS example from the CodePlex All-In-One Code Framework Samples that does this: (we are trying this now)
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern SafePipeHandle CreateNamedPipe(string pipeName,
PipeOpenMode openMode, PipeMode pipeMode, int maxInstances,
int outBufferSize, int inBufferSize, uint defaultTimeout,
SECURITY_ATTRIBUTES securityAttributes);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool ConnectNamedPipe(SafePipeHandle hNamedPipe,
IntPtr overlapped);
Does anyone have an idea for a direction? Could it be security related? Thanks.
回答1:
The code sample provided by MS All-In-One Code Framework (C# named-pipe server for IPC (CSNamedPipeServer)) shows the correct way how to do this and it works on Win 7.
来源:https://stackoverflow.com/questions/7228254/problem-migrating-connectnamedpipe-from-xp-to-win-7