我需要在C# NT Service中使用FindWindow, FindWindowEx系统获取特定窗口的句柄,但总是失败.而相同的代码放到Console Application和Window Application中都可以正常取得,这是为什么??难道NT Service中不允许使用FindWindow,FindWindowEx函数,我试过GetDesktopWindow,是可以正常获得桌面句柄的.高人们帮忙解决问题啊~~~,偶郁闷死了,原以来一挺简单的东西,结果搞得一头雾水~~对了,失败后我用GetLastError函数取得的错误代码为126(找不到模块),更让偶摸不着头脑,郁闷~~~

解决方案 »

  1.   

    按照常理是不可能的,首先要Dllinport那个函数所在的DLL
      

  2.   

    郁闷~~~,发现用PostMessage发送广播消息都不行~~
    PostMessage(HWND_BROADCAST, WM_CLOSE, 0,0);
    得到的错误代码为127(找不到指定的程序)
    郁闷啦,,,救偶啊~~~
      

  3.   

    给你段代码 , 在安装服务的时候即打开该选项:
    using System.Management;
    private void ProjectInstaller_AfterInstall(object sender, System.Configuration.Install.InstallEventArgs e)
    {
    ConnectionOptions coOptions = new ConnectionOptions();
    coOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2",coOptions);
    mgmtScope.Connect(); ManagementObject wmiService;
    wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'");
    ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
    InParam["DesktopInteract"] = true;
    ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
    }