我正在尝试编写一个简单的程序来连接到远程计算机并查询索引状态。
这是在我的机器上执行此操作的代码。这工作正常。
using System;
using Microsoft.Search.Interop;
namespace IndexStatus
{
class Program
{
static void Main(string[] args)
{
CSearchManager manager = new CSearchManager();
CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
_CatalogPausedReason pReason;
_CatalogStatus pStatus;
Console.WriteLine(catalogManager.NumberOfItems().ToString());
int plIncrementalCount;
int plNotificationQueue;
int plHighPriorityQueue;
catalogManager.NumberOfItemsToIndex(out plIncrementalCount, out plNotificationQueue, out plHighPriorityQueue);
Console.WriteLine(plIncrementalCount.ToString());
Console.WriteLine(plNotificationQueue.ToString());
Console.WriteLine(plHighPriorityQueue.ToString());
catalogManager.GetCatalogStatus(out pStatus, out pReason);
Console.WriteLine(pStatus.ToString() + " " + pReason.ToString());
Console.ReadLine();
}
}
}
然而,当我打电话时GetCatalog
on "mycomputername.SystemIndex"
代替"SystemIndex"
, I get
类型为“System.Runtime.InteropServices.COMException”的未处理异常发生在
索引状态.exe
附加信息:HRESULT 异常:0x80042103
Visual Studio 2015 在 Windows 8.1 上以管理员权限运行。目标计算机主要是Windows 7系统,程序主要在Windows 10系统上运行。我正在使用从 Microsoft Windows Search 3.X SDK 下载的 Microsoft.Search.Interop.dllhere https://www.microsoft.com/en-us/download/details.aspx?id=7388。我关闭了防火墙以防万一与此有关,但显然不是。
我已经检查过,如果我完全无意义地调用该函数,我会得到相同的异常,例如"sdfd"
。我发现this http://joshpoley.blogspot.com/2011/09/hresults-user-0x004.html:
MSS_E_CATALOGNOTFOUND - 0x80042103 - (8451) WindowsSearchErrors.h
The specified catalog was not found. Check to see if it was deleted, or if there are errors in your application code.
我尝试使用“localhost”而不是计算机名称,但这没有帮助。
The MSDN 文档 https://msdn.microsoft.com/en-us/library/windows/desktop/bb231475(v=vs.85).aspx说这个:
目前 Microsoft Windows Desktop Search (WDS) 3.0 仅支持一种
目录,它被命名为 SystemIndex。
我不知道如何理解这一点。也许该方法无法在不同的机器之间进行选择?如果是这样,除了使用 PsExec 之类的东西之外,还有其他方法可以连接到远程目录并进行这些查询吗?
Re Ben N 的回答:这对我来说开始变得很深,但我更着迷而不是害怕。 :) 经过几次修改后,您的代码对我有用:
CSearchManagerClass manager = System.Runtime.InteropServices.Marshal.CreateWrapperOfType(comManager, typeof(CSearchManagerClass));
无法在 Visual Studio 2015 上编译,并会出现以下错误:
第二个错误很容易通过添加强制转换来修复:
CSearchManagerClass manager = (CSearchManagerClass)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(comManager, typeof(CSearchManagerClass));
至于“互操作类型无法嵌入”错误消息,我发现这个问题 https://stackoverflow.com/questions/2483659/interop-type-cannot-be-embedded。有两种建议的解决方案:
第一种方案可以使程序编译通过,但影响可移植性。现在该程序将无法在没有 .dll 的计算机上运行。第二个解决方案编译但抛出
类型为“System.ArgumentException”的未处理异常发生于
mscorlib.dll
附加信息:类型必须是 __ComObject 或派生的
来自 __ComObject。
当我在自己的机器上运行它时,就在这条线上。
但还有一个问题,这个我不知道。当我在同事的机器上运行它时(我是他计算机上的管理员,Visual Studio 正在以管理员权限运行),我得到
“System.UnauthorizedAccessException”类型的未处理异常
发生在 mscorlib.dll 中
附加信息:检索远程的 COM 类工厂
具有 CLSID {7D096C5F-AC08-4F1F-BEB7-5C22C517CE39} 的组件
由于以下错误,计算机计算机名失败:80070005
计算机名。
This does scare me a little bit because I know next to nothing about COM. I've checked that DCOM is enabled on his computer and on mine. But when I try to go to his computer in Component Services it shows as a and DCOM Config is missing from the tree. And the same happens for other computers on the domain (even though I have admin rights on all workstations). This blog https://hurcane.wordpress.com/2008/06/11/remote-administration-of-com-through-a-firewall/ suggests it could be a firewall issue and if it is, it's not something that will be feasible to overcome.
您的两个答案都绝对值得赏金,但如果您有任何建议或能够阐明正在发生的事情,我将非常感激。如果我不能让它发挥作用,那也没关系,但我绝对想从中获取尽可能多的知识。