How to pass IntPtr between process/apps

我与影子孤独终老i 提交于 2019-12-24 16:46:04

问题


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!


回答1:


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.




回答2:


You can use Microsoft Message Queuing for this purpose.

Another way is you can find your window by it's title without passing an IntPtr to it as mentioned here.



来源:https://stackoverflow.com/questions/36953129/how-to-pass-intptr-between-process-apps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!