C# CreatePipe() -> Protected memory error

Posted by M. Dimitri on Stack Overflow See other posts from Stack Overflow or by M. Dimitri
Published on 2010-03-29T11:24:32Z Indexed on 2010/03/29 12:03 UTC
Read the original article Hit count: 636

Filed under:
|
|
|

Hi all,

I trying to create a pipe using C#. The code is quite simple but I get a error saying "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Here the COMPLETE code of my form :

public partial class Form1 : Form
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool CreatePipe(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);

    [StructLayout(LayoutKind.Sequential)]
    public struct SECURITY_ATTRIBUTES
    {
        public DWORD nLength;
        public IntPtr lpSecurityDescriptor;
        public bool bInheritHandle;
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void btCreate_Click(object sender, EventArgs e)
    {
        SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
        sa.nLength = (DWORD)System.Runtime.InteropServices.Marshal.SizeOf(sa);
        sa.lpSecurityDescriptor = IntPtr.Zero;
        sa.bInheritHandle = true;

        SafeFileHandle hWrite = null;
        SafeFileHandle hRead = null;

        if (CreatePipe(out hRead, out hWrite, sa, 4096))
        {
            MessageBox.Show("Pipe created !");
        }
        else
            MessageBox.Show("Error : Pipe not created !");
    }   
}

At the top I declare : using DWORD = System.UInt32;

Thank you very much if someone can help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about api