IPC between .NET and C ++ Applications

Are there libraries for interprocess communication (IPC) between a .NET application and its own C ++ application?

+4
c ++ ipc
Jan 6 '10 at 10:27
source share
4 answers

You can use Socket for easy communication. This is on the OS, so you do not need new libraries. Details in C ++ Socket and C # Socket

If interprocess communication will always run on the same computer, named pipes are the way to go because they are faster than others.

+4
Jan 06
source share

A simple (albeit limited) IPC mechanism is the WM_COPYDATA message

You can easily use it to transfer structure to your own application.

I use the following helper class:

public static class CopyDataHelper { [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { private int _dwData; private int _cbData; private IntPtr _lpData; public int DataId { get { return _dwData; } set { _dwData = value; } } public int DataSize { get { return _cbData; } } public IntPtr Data { get { return _lpData; } } public void SetData<T>(T data) where T : struct { int size = Marshal.SizeOf(typeof(T)); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(data, ptr, true); _lpData = ptr; _cbData = size; } public T GetData<T>() where T : struct { return (T)Marshal.PtrToStructure(_lpData, typeof(T)); } } [DllImport("User32.dll")] private static extern bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam); public const int WM_COPYDATA = 0x004A; public static bool Send<T>(IntPtr fromHwnd, IntPtr toHwnd, int dataId, T data) where T : struct { IntPtr ptr = IntPtr.Zero; try { COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.DataId = dataId; cds.SetData(data); return SendMessage(toHwnd, WM_COPYDATA, fromHwnd, ref cds); } finally { if (ptr != IntPtr.Zero) Marshal.FreeHGlobal(ptr); } } public static COPYDATASTRUCT Receive(Message msg) { if (msg.Msg != WM_COPYDATA) throw new ArgumentException("This is not a WM_COPYDATA message"); COPYDATASTRUCT cds = (COPYDATASTRUCT)msg.GetLParam(typeof(COPYDATASTRUCT)); return cds; } } 

To catch the WM_COPYDATA message, you need to override WndProc :

  protected override void WndProc(ref Message msg) { if (msg.Msg == CopyDataHelper.WM_COPYDATA) { CopyDataHelper.COPYDATASTRUCT cds = CopyDataHelper.Receive(msg); if (cds.DataId == myDataId) { MyData data = cds.GetData<MyData>(); msg.Result = DoSomething(data); return; } } base.WndProc(ref msg); } 
+3
Jan 06 '10 at 11:02
source share

Check the Google protocol buffers ( protobuf ). The original implementation supports C ++, Java, and Python, but there is protobuf-net for .NET.

+3
Apr 18 '10 at 18:36
source share

Named pipes or I used COM Interop are good options.

+1
Jan 6 '10 at 11:18
source share



All Articles