简体   繁体   English

Windows服务与WMI

[英]Windows Service with WMI

For one of my customers, I'm writing a Windows Service in C#. 对于我的一位客户,我正在用C#编写Windows服务。 It checks a lot of stuff, but 1 thing it has to do, is check if a printerport (ip address) already exists. 它检查了很多东西,但是要做的一件事是检查是否已经存在一个打印机端口(IP地址)。 if yes, connect certain printer to the ip-port. 如果是,将某些打印机连接到ip端口。 If no, create port and connect after all. 如果否,则创建端口并毕竟连接。

I use WMI to check for existing printer ports and to create them. 我使用WMI检查现有的打印机端口并创建它们。 I also use WMI to connect the printer to the printerport. 我还使用WMI将打印机连接到打印机端口。

On Windows 7, this works like a charm. 在Windows 7上,这就像一个超级按钮。 But on my (virtual) XP machine, the service doesn't seem to have any WMI rights at all. 但是在我的(虚拟)XP计算机上,该服务似乎根本没有任何WMI权限。 The port check fails and the creating fails. 端口检查失败,创建失败。 Just a simple 'access denied' from the ManagementException is given. 仅给出了来自ManagementException的简单“拒绝访问”。

If I make a little testprogram (console application in C#), it works like a charm as well. 如果我编写一些测试程序(C#中的控制台应用程序),它的工作原理也很吸引人。

I tried running the service under different accounts (local system, administrator, my personal login with admin rights), nothing seems to work and I'm getting the feeling you cannot use WMI in a Windows Service on Windows XP. 我尝试使用其他帐户(本地系统,管理员,具有管理员权限的个人登录名)运行该服务,但似乎没有任何效果,并且我感觉到您无法在Windows XP的Windows Service中使用WMI。

Do you guys have any clue how to fix this? 你们有什么线索解决这个问题吗? Or what is wrong here ? 还是这里有什么问题?

Don't know where else to look for answers, so I made an account here, hoping to find the right answer with you. 不知道在哪里可以找到答案,所以我在这里注册了一个帐户,希望能找到合适的答案。

Are you using Win32_Printer? 您正在使用Win32_Printer吗? On the Win32_Printer reference page, it says, Win32_Printer参考页上,它说,

Remarks 备注

The Win32_Printer class is derived from CIM_Printer. Win32_Printer类是从CIM_Printer派生的。 Before calling SWbemObject.Put_ or IWbemServices::PutInstance for a Win32_Printer instance, the SeLoadDriverPrivilege privilege (wbemPrivilegeLoadDriver for Visual Basic and LoadDriver for scripting monikers) must be enabled. 在为Win32_Printer实例调用SWbemObject.Put_或IWbemServices :: PutInstance之前,必须启用SeLoadDriverPrivilege特权(对于Visual Basic为wbemPrivilegeLoadDriver,对于脚本别名为LoadDriver)。 For more information, see Privilege Constants and Executing Privileged Operations. 有关更多信息,请参见特权常量和执行特权操作。 The following VBScript code example shows how to enable the SeLoadDriverPrivilege privilege in script. 下面的VBScript代码示例显示如何在脚本中启用SeLoadDriverPrivilege特权。

Edit: I know there a difference between Windows Server 2003 and Server 2008 when it comes to services. 编辑:我知道Windows Server 2003和Server 2008之间在服务方面有所不同。 Under the older operating systems (maybe XP is included), the user's profile is not loaded by default. 在较旧的操作系统(可能包括XP)下,默认情况下不会加载用户的配置文件。 This has issues when trying to use DPAPI APIs. 尝试使用DPAPI API时出现问题。 Perhaps, something similar is occuring in this situation. 也许在这种情况下正在发生类似的事情。 Only a guess. 只是一个猜测。

Edit: Ensure you are enabling Impersonation when connecting to WMI. 编辑:确保连接到WMI时启用模拟。 There is an example on the Win32_TCPIPPrinterPort page. Win32_TCPIPPrinterPort页面上有一个示例。

ConnectionOptions options = new ConnectionOptions();
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

ManagementScope scope = new ManagementScope(@"\\root\\cimv2", options);
scope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where HostAddress = '" + printerPort + "'"); 

There are two things you could try, first off you can try and set some credentials on your ConnectionOptions 您可以尝试两种方法,首先可以尝试在ConnectionOptions上设置一些凭据

ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;
options.EnablePrivileges = true;

The other approach could be by wrapping your entire code in some impersonation trickery, but I would definately try looking at the ConnectionOptions first. 另一种方法可能是将整个代码包装在一些模拟欺骗中,但是我一定会尝试首先查看ConnectionOptions。

If you want to try the impersonation bit I wrote a blogpost about it a while back. 如果您想尝试模仿,我前不久就写了一篇博客文章。 http://beddet.wordpress.com/2011/09/25/remote-controlling/ http://beddet.wordpress.com/2011/09/25/remote-controlling/

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

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