我使用 VS2008 开发了一个 excel 2007 插件,在插件中我想使用 Activation Context API 来实例化一个 COM 类。
奇怪的是我可以在 Window 7 上成功实例化 COM 类,但在 Windows XP/2003 上 buf 失败。
这是代码片段
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 中查找 ComViewer.dll 而不是我在 lpAssemblyDirectory 中指定的目录。
任何人都可以帮忙吗?提前致谢。