Click here to Skip to main content
15,896,444 members
Articles / Desktop Programming / Win32

Inter-Process Communication (IPC) Introduction and Sample Code

Rate me:
Please Sign up or sign in to vote.
4.91/5 (57 votes)
19 Dec 2009Ms-PL8 min read 251.6K   12.2K   195  
This article will cover general IPC technologies in All-In-One Code Framework. The IPC technologies include Named Pipes, File Mapping, MailSlot, etc.
/****************************** Module Header ******************************\
* Module Name:	ClientForm.cs
* Project:		CSReceiveWM_COPYDATA
* Copyright (c) Microsoft Corporation.
* 
* IPC based on the Windows message WM_COPYDATA is a mechanism for exchanging 
* data among Windows applications in the local machine. The receiving 
* application must be a Windows application. The data being passed must not 
* contain pointers or other references to objects not accessible to the 
* application receiving the data. While WM_COPYDATA is being sent, the 
* referenced data must not be changed by another thread of the sending 
* process. The receiving application should consider the data read-only. The 
* lParam parameter is valid only during the processing of the message. The 
* receiving application should not free the memory referenced by lParam. If 
* the receiving application must access the data after SendMessage returns, 
* it needs to copy the data into a local buffer.
* 
* CSSendWM_COPYDATA demonstrates how a sending process passes the data to 
* other Windows applications by using SendMessage and WM_COPYDATA.
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* History:
* * 3/17/2009 08:50 PM Riquel Dong Created
* * 3/19/2009 12:16 AM Jialiang Ge Reviewed
\***************************************************************************/

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
#endregion


namespace CSSendWM_COPYDATA
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();
        }

        private void bnSendMessage_Click(object sender, EventArgs e)
        {
            int nNumber;
            // Validate the parameter value in tbNumber
            if (!int.TryParse(this.tbNumber.Text, out nNumber))
            {
                MessageBox.Show("Invalid value of nNumber!");
                return;
            }


            /////////////////////////////////////////////////////////////////
            // Find the target Window Handle.
            // 

            IntPtr hTargetWnd = WindowNative.FindWindow(null, "CSReceiveWM_COPYDATA");

            // Validate the Window Handle.
            if (hTargetWnd == IntPtr.Zero)
            {
                MessageBox.Show("Unable to find the target Window!");
                return;
            }


            /////////////////////////////////////////////////////////////////
            // Prepare the COPYDATASTRUCT struct with the data to be sent.
            // 

            // Declare the MyStruct struct to hold the message
            MyStruct myStruct;
            myStruct.nNumber = nNumber;
            myStruct.strMessage = this.tbMessage.Text;

            // Marshals from the managed object to a native block of memory
            IntPtr ptrMyStruct = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct));
            Marshal.StructureToPtr(myStruct, ptrMyStruct, true);

            // Declare the COPYDATASTRUCT struct for the WM_COPYDATA message.
            COPYDATASTRUCT cds = new COPYDATASTRUCT();
            cds.cbData = Marshal.SizeOf(myStruct);
            cds.lpData = ptrMyStruct;


            /////////////////////////////////////////////////////////////////
            // Send the COPYDATASTRUCT struct through the WM_COPYDATA message 
            // to the receiving Window.
            // 

            // Send the WM_COPYDATA message
            // The application must use SendMessage, instead of PostMessage 
            // to send WM_COPYDATA because the receiving application must 
            // accept while it is guaranteed to be valid.
            WindowNative.SendMessage(
                hTargetWnd,                 // Handle of the target Window
                WindowNative.WM_COPYDATA,   // WM_COPYDATE message
                this.Handle,                // Handle of the current Window
                ref cds                     // COPYDATASTRUCT structure
                );

            // Check error of SendMessage
            int result = Marshal.GetLastWin32Error();
            if (result != 0)
            {
                MessageBox.Show(String.Format(
                    "SendMessage failed w/err 0x{0:X}", result));
            }


            /////////////////////////////////////////////////////////////////
            // Clean up.
            // 

            // Frees the native memory allocated for MyStruct
            Marshal.FreeHGlobal(ptrMyStruct);
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    internal struct MyStruct
    {
        public int nNumber;

        /// <summary>
        /// TCHAR[256]
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string strMessage;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
China China
Microsoft All-In-One Code Framework delineates the framework and skeleton of Microsoft development techniques through typical sample codes in three popular programming languages (Visual C#, VB.NET, Visual C++). Each sample is elaborately selected, composed, and documented to demonstrate one frequently-asked, tested or used coding scenario based on our support experience in MSDN newsgroups and forums. If you are a software developer, you can fill the skeleton with blood, muscle and soul. If you are a software tester or a support engineer like us, you may extend the sample codes a little to fit your specific test scenario or refer your customer to this project if the customer's question coincides with what we collected.
http://cfx.codeplex.com/

Comments and Discussions