给梦一个奔跑的方向!
PDF Print E-mail
User Rating: / 0
PoorBest 
Written by xlingfairy
Tuesday, 04 May 2010 20:21
设想的是在 Server 端收到 Client 发送来的命令后,打开对应的窗口(比如客户发送 Notepad ,Server 端就会打开一个 Notepad)。
 
如果是 ConsoleApplication 好办,参见:
 
 
但是把 Server 端做成一个 windows 服务(service), 需要处理一下这个问题:
 
windows 服务 调用 Process ,有进程,无窗口
 
第一次写 windows service 程序,运行结果并不像 ConsoleApplication 那样,执行成功,但是没有任何窗口打开。
 
一开始我看网上有猪头说是因为没设线程,service 开始,onstart之后,程序就结束了,就像 ConsoleApplication 的结尾没加 Console.Read() 一样。
 
按这猪头的说法,我加了一个死循环,放了 Thread.Sleep, 但结果仍然一样。
 
后来看到有人问这个问题,才发现 任务管理器 里有好多个 Notepad ,CMD 进程,只是没有窗口而以。
 
那么是什么原因,只有进程,而没有窗口呢?
 
在服务的属性里有个登陆选项卡,有个 允许服务与桌面交互 选项,勾上它,重启服务。在运行 Client 端,输入命令,对应的窗口就打开了。
 
 
但是要怎么用程序把这个选项给选上呢?有说注册服务之前改注册表的,有说用 PInvoke 的,我没有试,水平有限,不宜拉网太大。
 
我找到了一个比较简单的方法:
http://topic.csdn.net/t/20041103/10/3516549.html
5楼
 
语法需要修改一点:
 
。。。
using System.ServiceProcess;
using System.Management;
。。。
 
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) {
    ConnectionOptions coOptions = new ConnectionOptions();
    coOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
    mgmtScope.Connect();
 
    ServiceController sc = new ServiceController( this.serviceInstaller1.ServiceName );
 
    ManagementObject wmiService;
    wmiService = new ManagementObject("Win32_Service.Name='" + sc.ServiceName + "'");
    ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
    InParam["DesktopInteract"] = true;
    ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
    sc.Start();
}
 

Add comment


Security code
Refresh

Popular Contents

Recommend

Site Info

Members : 1
Content : 141
Web Links : 7
Content View Hits : 112544

Links