本文介绍了如何在进程/应用之间传递IntPtr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个申请.其中一个是WPF应用程序,另一个是sys tray应用程序.第一个应该以某种方式将其窗口指针IntPtr

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;

第二个应该接受它并检测WPF应用程序的活动屏幕.

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

Screen activeScreen = Screen.FromHandle(thisWindowHandle);

我的问题是我们如何在这些应用之间发送IntPtr,并且有可能做到吗?

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

谢谢!

推荐答案

可以在同一桌面的同一会话中运行的进程之间交换窗口句柄.如果您的应用是服务,那将是行不通的.

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.

您需要使用标准的.NET进程互操作机制来传递句柄,例如WCF,套接字,命名管道,文件或命令行参数.也许您所需要的只是Process.MainWindowHandle,所以您根本不必传递任何东西,这当然是最简单的.

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.

请牢记故障模式,在这种情况下,WPF应用程序崩溃且托盘应用程序"继续运行的情况很少出现.否则,在通知区域中显示图标不需要单独的过程.

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.

这篇关于如何在进程/应用之间传递IntPtr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:45