5

这是最近在 server2003 和 2008r2 上开始发生的异常:

ManagementException: Generic failure
Stack:    at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
  at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()

收到此错误后,我还看到发生此异常(后台处理程序已停止?):

The RPC server is unavailable

Win32Exception: The RPC server is unavailable
Stack:    at System.Drawing.Printing.PrinterSettings.get_InstalledPrinters()

此处列出了生成 WMI 查询的代码:

// printername:   \\server\printer  or   mylocalprinter

PrinterAttributes attribs = PrinterAttributes.Hidden;
ManagementObjectSearcher searchPrinter = null;
ManagementObjectCollection prntCol = null;

try 
{
    // "SELECT Attributes, Name FROM Win32_Printer"
    string qry = string.Format("SELECT Attributes, Name FROM Win32_Printer WHERE Name = \'{0}\'", printerName);
    searchPrinter = new ManagementObjectSearcher(qry);
    prntCol = searchPrinter.Get();

    foreach (ManagementObject prnt in prntCol) {
      if (prnt["Name"].ToString() == printerName) { /* ... */ }
    }
}
finally 
{
    if (prntCol != null) prntCol.Dispose();
    if (searchPrinter != null) searchPrinter.Dispose();
}

// ...

进行搜索后,我发现这些链接可以给我一个可能的提示:

  1. https://stackoverflow.com/a/5247725/187650
  2. http://social.msdn.microsoft.com/Forums/vstudio/en-US/b20c35f7-375b-4b67-af2e-bd432b6915da/how-to-retrieve-all-printer-names-installed-in-a-pc
  3. http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.wmi/2005-12/msg00097.html
  4. http://www.vistax64.com/powershell/134250-get-wmiobject-generic-failure.html

即使我尝试在 PowerShell 中对远程 pc 执行此查询:

Get-WmiObject -Query "SELECT Attributes, Name FROM Win32_Printer" -ComputerName "remotepc"

一段时间后我收到此错误:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:1
+ Get-WmiObject -Query "SELECT Name FROM Win32_Printer" -ComputerName "remotepc"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

是否有任何 WMI 专家可以分享对 C# 代码的改进,因为目前我不知道为什么它只会偶尔发生一次。

4

0 回答 0