Search code examples
c#.netintptr

How to pass IntPtr between process/apps


I have got 2 application. One of them is a WPF app and another is sys tray app. The first one should somehow pass to another one his window pointer IntPtr

IntPtr  thisWindowHandle = (new WindowInteropHelper(this)).Handle;

And the second one should accept it and detect the active screen of WPF app.

Screen activeScreen = Screen.FromHandle(thisWindowHandle);

My question is how we can send IntPtr between those apps and is it possible to do at all?

Thank you!


Solution

  • It is possible, window handles can be exchanged between processes that run in the same session on the same desktop. It wouldn't work if your app is, say, a service.

    You need to use the standard .NET process interop mechanisms to pass the handle, like WCF, a socket, a named pipe, a file or a command line argument. Maybe all that you need is Process.MainWindowHandle so you don't have to pass anything at all, that is easiest of course.

    Do keep the failure modes in mind, a scenario where your WPF app crashes and your "tray app" continues to run rarely turns out well. Displaying an icon in the notification area does not otherwise require a separate process.