简体   繁体   English

如何使用C#将消息从Windows服务传递到Windows桌面应用程序?

[英]How to pass a message from windows service to windows desktop application using c#?

I want to pass a message from windows service to an windows desktop application that is already running. 我想将消息从Windows服务传递到已经运行的Windows桌面应用程序。 I have implemented a timer on windows service. 我已经在Windows服务上实现了一个计时器。 after an interval the service send a message to the windows application. 间隔后,服务将消息发送到Windows应用程序。

The service or sender code is below: 服务或发件人代码如下:

System.Diagnostics.Process[] lProcs = System.Diagnostics.Process.GetProcessesByName("TestProcess2");

        if (lProcs.Length > 0)
        {
            IntPtr handle = lProcs[0].MainWindowHandle;

            if (handle != IntPtr.Zero)
                SendMessage(handle, 232, IntPtr.Zero, IntPtr.Zero);
        }

and the windows desktop application (receiver) code are as follows: Windows桌面应用程序(接收器)代码如下:

protected override void WndProc(ref Message m)
    {
        if (m.Msg == 232)
        {
            MessageBox.Show("Received");
        }
        else
        {
            base.WndProc(ref m);
        }
    }

the above code is working fine when both process are windows desktop application. 当两个进程都是Windows桌面应用程序时,以上代码工作正常。 when i used windows service as a sender then the windows desktop application process can not receive the message. 当我使用Windows服务作为发件人时,Windows桌面应用程序进程无法接收该消息。 Can you help me please? 你能帮我吗?

The service and the desktop application are running in two different Window Stations. 该服务和桌面应用程序在两个不同的Window Station中运行。 For security reasons it is impossible to send window messages between applications running in separate Window Stations. 出于安全原因,不可能在单独的Window Station中运行的应用程序之间发送窗口消息。

In order to communicate between a service and a desktop application you must use some sort of inter-process communication method (good possibilities are sockets, named pipes, DCOM, etc.) or some framework running on top of one of these, such as Remoting or WCF. 为了在服务和桌面应用程序之间进行通信,您必须使用某种进程间通信方法(很可能是套接字,命名管道,DCOM等)或在其中之一之上运行的某些框架,例如Remoting或WCF。

I suspect this is because the two processes are running under different user accounts. 我怀疑这是因为两个进程在不同的用户帐户下运行。

I think you need to use the change message filter function to allow it to be received, see here: 我认为您需要使用更改消息过滤器功能来接收它,请参见此处:

ChangeWindowMessageFilterEx function ChangeWindowMessageFilterEx函数

One way to do this, is to host a WCF interface in your service. 一种方法是在您的服务中托管WCF接口。 This then allows communication between (potentially) any application and the service. 然后,这允许(潜在地)任何应用程序和服务之间进行通信。

Check out these links for detailed examples: 查看这些链接以获取详细示例:

http://www.codeproject.com/Articles/38160/WCF-Service-Library-with-Windows-Service-Hosting http://msdn.microsoft.com/en-us/library/ms733069.aspx http://www.codeproject.com/Articles/38160/WCF-Service-Library-with-Windows-Service-Hosting http://msdn.microsoft.com/en-us/library/ms733069.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM