srm.exe安装shell扩展:失败,显示“无法转换基础导出值”

时间:2014-08-22 06:11:50

标签: shell-extensions sharpshell

我用SharpShell编写了这个简单的shell扩展(explorer上下文菜单):

[ComVisible(true)]
[COMServerAssociation(AssociationType.AllFiles)]
public class SampleExtension : SharpContextMenu
{
    protected override bool CanShowMenu()
    {
        return true;
    }
    protected override ContextMenuStrip CreateMenu()
    {
        var menu = new ContextMenuStrip();
        var item = new ToolStripMenuItem
        {
            Text = "Hello world!"
        };
        menu.Items.Add(item);
        return menu;
    }
}

它可以在SharpShell服务器管理器的调试中工作,但是当我尝试在命令行上通过srm.exe安装它时,我得到:

srm.exe install ..\SampleExtension\bin\Debug\CountLinesExtension.dll -codebase  

System.ComponentModel.Composition.CompositionContractMismatchException was unhandled
  HResult=-2146233088
  Message=Cannot cast the underlying exported value of type 'SharpShell.SharpShellServer (ContractName="SharpShell.ISharpShellServer")' to type 'SharpShell.ISharpShellServer'.
  Source=System.ComponentModel.Composition
  StackTrace:
       at System.ComponentModel.Composition.ExportServices.CastExportedValue[T](ICompositionElement element, Object exportedValue)
       at System.ComponentModel.Composition.ExportServices.GetCastedExportedValue[T](Export export)
       at System.ComponentModel.Composition.ExportServices.<>c__DisplayClassa`1.<CreateStronglyTypedLazyOfT>b__7()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Lazy`1.get_Value()
       at ServerRegistrationManager.Application.<LoadServerTypes>b__2(Lazy`1 st)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at ServerRegistrationManager.Application.InstallServer(String path, RegistrationType registrationType, Boolean codeBase)
       at ServerRegistrationManager.Application.Run(String[] args)
       at ServerRegistrationManager.Program.Main(String[] args)

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我们遇到了同样的问题。结果我们只是没有从与我们的dll相同的目录运行srm.exe。我们的dll包括其他dll,因此它无法加载。一旦我们将srm.exe与我们的dll一起运行在同一目录中,它就可以工作。