private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "STOP")
            {
                btnStart.Text = "START";

                memoryTimer.Stop();

                rbServer.Enabled = true;
                rbClient.Enabled = true;

                Thread.Sleep(100);

                FileMappingNative.UnmapViewOfFile(pBuf);
                FileMappingNative.CloseHandle(hMapFile);
            }
            else
            {
                btnStart.Text = "STOP";

                rbServer.Enabled = false;
                rbClient.Enabled = false;

                if (workType == 'S')
                {
                    // Create the file mapping object
                    hMapFile = FileMappingNative.CreateFileMapping(
                        (IntPtr)FileMappingNative.INVALID_HANDLE_VALUE,
                        IntPtr.Zero,
                        // 파일 Mapping 을 쓰기 가능으로 Open
                        FileProtection.PAGE_READWRITE,
                        0,
                        BUFFER_SIZE,
                        strMapFileName);

                    if (hMapFile == IntPtr.Zero)
                    {
                        Console.WriteLine("Unable to create file mapping object w/err 0x{0:X}",
                                          FileMappingNative.GetLastError());
                        return;
                    }
                    Console.WriteLine("The file mapping object, {0}, is created.",
                                      strMapFileName);


                    /////////////////////////////////////////////////////////////////////
                    // Maps the view of the file mapping into the address space of the
                    // current process.
                    //

                    // Create file view from the file mapping object.
                    // 버퍼에 매핑, 향후 pBuf 를 통하여 메모리에 쓸 수 있음
                    pBuf = FileMappingNative.MapViewOfFile(
                        hMapFile,
                        FileMapAccess.FILE_MAP_ALL_ACCESS,
                        0,
                        0,
                        BUFFER_SIZE);

                    if (pBuf == IntPtr.Zero)
                    {
                        Console.WriteLine("Unable to map view of file w/err 0x{0:X}",
                                          FileMappingNative.GetLastError());
                        return;
                    }
                    Console.WriteLine("The file view is created.");

                    memoryTimer.Start();
                }
                else if (workType == 'C')
                {
                    // Open the named file mapping.
                    hMapFile = FileMappingNative.OpenFileMapping(
                        FileMapAccess.FILE_MAP_ALL_ACCESS,
                        false,
                        strMapFileName);

                    if (hMapFile == IntPtr.Zero)
                    {
                        Console.WriteLine("Unable to open file mapping object w/err 0x{0:X}",
                                          FileMappingNative.GetLastError());
                        return;
                    }
                    Console.WriteLine("The file mapping object, {0}, is opened.",
                                      strMapFileName);

                    pBuf = FileMappingNative.MapViewOfFile(
                        hMapFile,
                        FileMapAccess.FILE_MAP_ALL_ACCESS,
                        0,
                        0,
                        BUFFER_SIZE);

                    if (pBuf == IntPtr.Zero)
                    {
                        Console.WriteLine("Unable to map view of file w/err 0x{0:X}",
                                          FileMappingNative.GetLastError());
                        FileMappingNative.CloseHandle(hMapFile);
                        return;
                    }
                    Console.WriteLine("The file view is created.");

                    memoryTimer.Start();
                }
            }
        }
Esempio n. 2
0
    static void Main(string[] args)
    {
        /////////////////////////////////////////////////////////////////////
        // Create a file mapping.
        //

        string strMapFileName = "Local\\HelloWorld";

        // Create the file mapping object
        IntPtr hMapFile = FileMappingNative.CreateFileMapping(
            (IntPtr)FileMappingNative.INVALID_HANDLE_VALUE,
            IntPtr.Zero,
            FileProtection.PAGE_READWRITE,
            0,
            BUFFER_SIZE,
            strMapFileName);

        if (hMapFile == IntPtr.Zero)
        {
            Console.WriteLine("Unable to create file mapping object w/err 0x{0:X}",
                              FileMappingNative.GetLastError());
            return;
        }
        Console.WriteLine("The file mapping object, {0}, is created.",
                          strMapFileName);


        /////////////////////////////////////////////////////////////////////
        // Maps the view of the file mapping into the address space of the
        // current process.
        //

        // Create file view from the file mapping object.
        IntPtr pBuf = FileMappingNative.MapViewOfFile(
            hMapFile,
            FileMapAccess.FILE_MAP_ALL_ACCESS,
            0,
            0,
            BUFFER_SIZE);

        if (pBuf == IntPtr.Zero)
        {
            Console.WriteLine("Unable to map view of file w/err 0x{0:X}",
                              FileMappingNative.GetLastError());
            return;
        }
        Console.WriteLine("The file view is created.");


        /////////////////////////////////////////////////////////////////////
        // Write message to the file view.
        //

        // Append '\0' at the end of the message considering the native C++
        // FileMapping client (CppFileMappingClient).
        string strMessage = "Message from first process.\0";

        byte[] bMessage = Encoding.Unicode.GetBytes(strMessage);

        // Write message to the file view.
        Marshal.Copy(bMessage, 0, pBuf, bMessage.Length);

        Console.WriteLine("The following message is written to the shared memory:");
        Console.WriteLine("\"{0}\"", strMessage);

        // Wait to stop the server.
        Console.WriteLine("Press any key to stop the server.");
        Console.Read();


        /////////////////////////////////////////////////////////////////////
        // Unmap the file view and close the file mapping objects.
        //

        FileMappingNative.UnmapViewOfFile(pBuf);
        FileMappingNative.CloseHandle(hMapFile);
    }
Esempio n. 3
0
    static void Main(string[] args)
    {
        /////////////////////////////////////////////////////////////////////
        // Try to open a named file mapping.
        //

        string strMapFileName = "Local\\HelloWorld";

        // Open the named file mapping.
        IntPtr hMapFile = FileMappingNative.OpenFileMapping(
            FileMapAccess.FILE_MAP_ALL_ACCESS,
            false,
            strMapFileName);

        if (hMapFile == IntPtr.Zero)
        {
            Console.WriteLine("Unable to open file mapping object w/err 0x{0:X}",
                              FileMappingNative.GetLastError());
            return;
        }
        Console.WriteLine("The file mapping object, {0}, is opened.",
                          strMapFileName);


        /////////////////////////////////////////////////////////////////////
        // Maps the view of the file mapping into the address space of the
        // current process.
        //

        // Create file view from the file mapping object.
        IntPtr pBuf = FileMappingNative.MapViewOfFile(
            hMapFile,
            FileMapAccess.FILE_MAP_ALL_ACCESS,
            0,
            0,
            BUFFER_SIZE);

        if (pBuf == IntPtr.Zero)
        {
            Console.WriteLine("Unable to map view of file w/err 0x{0:X}",
                              FileMappingNative.GetLastError());
            FileMappingNative.CloseHandle(hMapFile);
            return;
        }
        Console.WriteLine("The file view is created.");


        /////////////////////////////////////////////////////////////////////
        // Show the message in shared memory.
        //

        // Copy all characters up to the first null char to strMessage
        string strMessage = Marshal.PtrToStringUni(pBuf);

        Console.WriteLine("The following message is read from the shared memory:");
        Console.WriteLine("\"{0}\"", strMessage);


        /////////////////////////////////////////////////////////////////////
        // Unmap the file view and close the file mapping objects.
        //

        FileMappingNative.UnmapViewOfFile(pBuf);
        FileMappingNative.CloseHandle(hMapFile);
    }