•   Posted in:
  • .NET

Recently I was asked to check why one of our applications crashes when it calls CreateNamedPipe function. Everything works fine in the x64 version, but the x86 version crashes. There are no differences in the code between x86 and x64 and they compiled the same.

Let’s see how CreateNamedPipe defined in our code:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern SafePipeHandle CreateNamedPipe(string lpName, uint dwOpenMode,
    uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize,
    uint nDefaultTimeOut, SECURITY_ATTRIBUTES securityAttributes);

and here is how it called:

SafePipeHandle safeHandle = CreateNamedPipe(
                $@"\\.\pipe\{pipeName}",
                cPipeAccessDuplex |
                cFileFlagOverlapped,
                cPipeTypeMessage |
                cPipeWait,
                cPipeUnlimitedInstances,
                nOutBufferSize: 0,
                nInBufferSize: 0,
                cWaitForever,
                securityAttr);

Everything looks correct. I checked flags, compared them with