gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:54:24 31 4
gpt4 key购买 nike

我想将消息从 Windows 服务传递到已在运行的 Windows 桌面应用程序。我在 Windows 服务上实现了一个计时器。一段时间后,服务向 Windows 应用程序发送一条消息。

服务或发件人代码如下:

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);
}

和windows桌面应用(receiver)代码如下:

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

当两个进程都是 Windows 桌面应用程序时,上面的代码工作正常。当我使用 Windows 服务作为发件人时,Windows 桌面应用程序进程无法接收消息。你能帮我吗?

最佳答案

服务和桌面应用程序在两个不同的窗口站中运行。出于安全原因,不可能在单独的窗口站中运行的应用程序之间发送窗口消息。

为了在服务和桌面应用程序之间进行通信,您必须使用某种进程间通信方法(很好的可能性是套接字、命名管道、DCOM 等)或在其中之一之上运行的某些框架,例如 Remoting 或 WCF。

关于c# - 如何使用 C# 将消息从 Windows 服务传递到 Windows 桌面应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12976140/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com