【问题标题】:Couldn't use Activation Context API from within Excel 2007 Add-in on Windows XP/2003?无法在 Windows XP/2003 上的 Excel 2007 插件中使用激活上下文 API?
【发布时间】:2010-07-29 03:59:57
【问题描述】:

我用VS2008开发了一个excel 2007插件,在插件里面我想用Activation Context API来实例化一个COM类。

奇怪的是我可以在 Window 7 上成功实例化 COM 类,但在 Windows XP/2003 上 buf 失败。

这是代码-sn-p

  string codeBase = this.GetType().Assembly.CodeBase;
  string asmFullPath = new Uri(codeBase).LocalPath;
  string comAssemblyPath = Path.GetDirectoryName(asmFullPath);

  ACTCTX ac = new ACTCTX();
  ac.cbSize = Marshal.SizeOf(typeof(ACTCTX));
  ac.lpAssemblyDirectory = comAssemblyPath;
  ac.lpSource = Path.Combine(comAssemblyPath, "ComViewer.x.manifest");
  ac.dwFlags = ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;

  IntPtr cookie;
  IntPtr hActCtx = CreateActCtxW(ref ac);
  if (ActivateActCtx(hActCtx, out cookie))
  {
    try
    {
      //instantiate COM class
      IComViewer = new ComViewerClass();

    }
    finally
    {
      DeactivateActCtx(0, cookie);
    }
  }
  else
  {
    //TODO: Error message.
  }

COM 是用 C++ 编写的,清单如下所示:

在 Windows 2003/XP 上,我发现加载项在 c:\program files\microsoft Office\Office 12 中而不是我在 lpAssemblyDirectory 中指定的目录中查找 ComViewer.dll。

有人可以帮忙吗?提前致谢。

【问题讨论】:

    标签: api vsto activation-context-api


    【解决方案1】:

    请注意,在 XP/2003 中,如果您将 COM 文件信息放在 lpSource 指向的根清单中,则激活上下文 API 不尊重 lpAssemblyDirectory,在这种情况下,Windows 只会在可执行文件所在的目录中搜索 COM 文件在。

    解决方法是创建另一个依赖于原始清单 ComViewer.x.manifest 的 mainfest,并将其传递给 lpSource。在上面的示例中,您可以传入以下清单:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <dependency>
    <dependentAssembly>
    <assemblyIdentity name="ComViewer.x" >
    </assemblyIdentity>
    </dependentAssembly>
    </dependency>
    </assembly>
    

    当然,您也应该将元素添加到 ComViewer.x.manifest。

    【讨论】:

    • 我已经尝试过您的方法,但无法弄清楚如何将清单链接在一起。您能否扩展关于“应该添加元素”的最后一句?很高兴看到两个清单及其名称和简短描述一切如何工作,例如谁打电话给谁,为什么打电话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多