日期:2014-05-18  浏览次数:20935 次

C#中windows 服务如何与运行的桌面程序通信
有做过windows 服务的没?
C#中windows 服务如何与运行的桌面程序通信
我想先FindWindow,然后SendMessage
这个在不同的桌面程序程序间是好用的
但是在服务中却没有反映,应该怎么用呢???


------解决方案--------------------
可以用Socket,扩展性也好。

其他方法还有:
WCF(或以前的.Net Remoting)
Message Queue
共享内存
命名管道

用窗口消息不好,原因是
1、Windows Service一般运行在特权帐号下,暴露出一个窗口容易招来用SendMessage的攻击。
2、Vista下不同特权的程序运行在不同的Security level下,安全等级低级的窗口不能向安全等级高的窗口SendMessage,导致SendMessage完全不能用。


------解决方案--------------------
C# code

服务安装后事件 这样就可以了
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {
            ManagementBaseObject inPar = null;
            ManagementClass mc = new ManagementClass("Win32_Service");
            foreach (ManagementObject mo in mc.GetInstances())
            {
                if (mo["Name"].ToString() == "KeyboardService")
                {
                    inPar = mo.GetMethodParameters("Change");
                    inPar["DesktopInteract"] = true;
                    mo.InvokeMethod("Change", inPar, null);
                }
            }      
  }*/

------解决方案--------------------
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
ManagementBaseObject inPar = null;
ManagementClass mc = new ManagementClass("Win32_Service");
foreach (ManagementObject mo in mc.GetInstances())
{
if (mo["Name"].ToString() == "服务名")
{
inPar = mo.GetMethodParameters("Change");
inPar["DesktopInteract"] = true;
mo.InvokeMethod("Change", inPar, null);
}
}
}*/