Click here to Skip to main content
15,891,184 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.4K   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:	CSSimpleObject2.cs
* Project:		CSDllCOMServer
* Copyright (c) Microsoft Corporation.
* 
* This sample focuses on exposing .NET Framework components to COM, which 
* allows us to write a .NET type and consuming that type from unmanaged code 
* with distinct activities for COM developers.
* 
* CSSimpleObject2 - [Explicitly Define a Class Interface]
* 
* Program ID: CSDllCOMServer.CSSimpleObject2
* CLSID_CSSimpleObject2: 4B65FE47-2F9D-37B8-B3CB-5BE4A7BC0926
* IID_ICSSimpleObject2: 32DBA9B0-BE1F-357D-827F-0196229FA0E2
* DIID_ICSSimpleObject2Events: 95DB823B-E204-428c-92A3-7FB29C0EC576
* LIBID_CSDllCOMServer: F0998D9A-0E79-4F67-B944-9E837F479587
* 
* Properties:
* // With both get and set accessor methods.
* public float FloatProperty
* 
* Methods:
* // HelloWorld returns a string "HelloWorld"
* public string HelloWorld();
* // GetProcessThreadID outputs the running process ID and thread ID
* public void GetProcessThreadID(out uint processId, out uint threadId);
* 
* Events:
* // FloatPropertyChanging is fired before new value is set to the 
* // FloatProperty property. The Cancel parameter allows the client to cancel 
* // the change of FloatProperty.
* void FloatPropertyChanging(float NewValue, ref bool Cancel);
* 
* -------
* The recommended way of modeling a .NET component to be exposed to the COM  
* aware clients is to do away with ClassInterface, and instead, explicitly 
* factor out the members to be exported into a separate interface, and have 
* the .NET component implement that interface. Using a Class Interface is a 
* quick and easy way to get the .NET component exposed to COM aware clients 
* (See CSSimpleObject1), but it is not the recommended way. 
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* History:
* * 2/2/2009 11:55 PM Jialiang Ge Created
\***************************************************************************/

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
#endregion


namespace CSDllCOMServer
{
    #region Interfaces

    // Dual interface by default. This allows the client to get the best of 
    // both early binding and late binding.
    //[InterfaceType(ComInterfaceType.InterfaceIsDual)]
    //[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    //[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("32DBA9B0-BE1F-357D-827F-0196229FA0E2")]          // IID
    // The public interface describing the COM interface of the coclass 
    public interface ICSSimpleObject2
    {
        #region Properties

        float FloatProperty { get; set; }

        #endregion

        #region Methods

        string HelloWorld();

        void GetProcessThreadID(out uint processId, out uint threadId);

        [ComVisible(false)]
        void HiddenFunction();

        #endregion
    }


    [Guid("95DB823B-E204-428c-92A3-7FB29C0EC576")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    // The public interface describing the events the coclass can sink
    public interface ICSSimpleObject2Events
    {
        #region Events

        [DispId(1)]
        void FloatPropertyChanging(float NewValue, ref bool Cancel);

        #endregion
    }

    #endregion

    [ClassInterface(ClassInterfaceType.None)]           // No ClassInterface
    [ComSourceInterfaces(typeof(ICSSimpleObject2Events))]
    [Guid("4B65FE47-2F9D-37B8-B3CB-5BE4A7BC0926")]      // CLSID
    //[ProgId("CSCOMServerDll.CustomCSSimpleObject2")]  // ProgID
    public class CSSimpleObject2 : ICSSimpleObject2
    {
        #region Properties

        /// <summary>
        /// The private members don't make it into the type-library and are 
        /// hidden from the COM clients.
        /// </summary>
        private float fField = 0;

        /// <summary>
        /// A public property with both get and set accessor methods.
        /// </summary>
        public float FloatProperty
        {
            get { return this.fField; }
            set
            {
                bool cancel = false;
                // Raise the event FloatPropertyChanging
                if (null != FloatPropertyChanging)
                    FloatPropertyChanging(value, ref cancel);
                if (!cancel)
                    this.fField = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// A public method that returns a string "HelloWorld".
        /// </summary>
        /// <returns>"HelloWorld"</returns>
        public string HelloWorld()
        {
            return "HelloWorld";
        }

        /// <summary>
        /// A public method with two outputs: the current process Id and the
        /// current thread Id.
        /// </summary>
        /// <param name="processId">[out] The current process Id</param>
        /// <param name="threadId">[out] The current thread Id</param>
        public void GetProcessThreadID(out uint processId, out uint threadId)
        {
            processId = NativeMethod.GetCurrentProcessId();
            threadId = NativeMethod.GetCurrentThreadId();
        }

        /// <summary>
        /// A hidden method (ComVisible = false)
        /// </summary>
        public void HiddenFunction()
        {
            Console.WriteLine("HiddenFunction is called.");
        }
        
        #endregion

        #region Events

        [ComVisible(false)]
        public delegate void FloatPropertyChangingEventHandler(float NewValue, ref bool Cancel);

        /// <summary>
        /// A public event that is fired before new value is set to the
        /// FloatProperty property. The Cancel parameter allows the client 
        /// to cancel the change of FloatProperty.
        /// </summary>
        public event FloatPropertyChangingEventHandler FloatPropertyChanging;

        #endregion
    }

} // namespace CSDllCOMServer

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