【发布时间】:2018-09-19 05:05:11
【问题描述】:
我有一个 C++ 应用程序,我正在编写一个新的 Outlook 加载项,我认为它将与 VSTO 一起使用。我想在那之间进行交流,我正在努力找出最好的方法。在MS docs 中,他们提到了如何使用RequestComAddInAutomationService 将您的COM 类公开给外部解决方案。我对 COM 很陌生,但我在网上阅读了一些内容并得到了以下解决方案。我读到您应该构建加载项(对于 x86 作为我的 Outlook 版本而不是 AnyCPU),获取创建的 .tlb 文件并使用 #import 指令将其转换为 .tlh,然后使用 @987654330 @.tlh 文件以具有适当的类型。
ThisAddin.cs
namespace FirstOutlookAddIn
{
public partial class ThisAddIn
{
Outlook.Inspectors inspectors;
private AddInUtilities gUtilities;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
inspectors = this.Application.Inspectors;
inspectors.NewInspector +=
new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
}
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.EntryID == null)
{
gUtilities.SetMailItem(mailItem);
mailItem.Subject = "This text was added by using code";
mailItem.Body = "This text was added by using code";
}
}
}
protected override object RequestComAddInAutomationService()
{
if (gUtilities == null)
gUtilities = new AddInUtilities();
return gUtilities;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
IAddInUtilities.cs
using System.Runtime.InteropServices;
namespace FirstOutlookAddIn
{
[ComVisible(true)]
public interface IAddInUtilities
{
void MyExportedFunction();
}
}
AddInUtilities.cs
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
namespace FirstOutlookAddIn
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : StandardOleMarshalObject, IAddInUtilities
{
Outlook.MailItem globalMailItem;
public void SetMailItem(Outlook.MailItem item) => globalMailItem = item;
public void MyExportedFunction()
{
globalMailItem.Body = "I was called from outside!";
}
}
}
main.cpp
//#import "FirstOutlookAddIn.tlb" named_guids raw_interfaces_only
#include <iostream>
struct IUnknown; // Workaround for "combaseapi.h(229): error C2187: syntax error: 'identifier' was unexpected here" when using /permissive-
#include <Objbase.h>
#include "Debug\FirstOutlookAddIn.tlh"
int main() {
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
FirstOutlookAddIn::IAddInUtilities* pIFace;
// create the object and obtain a pointer to the sought interface
auto res = CoCreateInstance(
FirstOutlookAddIn::CLSID_AddInUtilities,
nullptr,
CLSCTX_LOCAL_SERVER,
FirstOutlookAddIn::IID_IAddInUtilities,
(LPVOID*)&pIFace);
if (res != S_OK)
{
std::cout << "Failed with: " << res;
}
auto res1 = pIFace->MyExportedFunction(); // use the object
std::cout << "Res: " << res1;
pIFace->Release(); // free the object
CoUninitialize();
}
问题是CoCreateInstance 返回REGDB_E_CLASSNOTREG Class not registered。相关的注册表树如下所示:
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID{5008A102-08E5-3F59-AADD-03875524CAD0} = FirstOutlookAddIn.AddInUtilities 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID{5008A102-08E5-3F59-AADD-03875524CAD0}\InprocServer32: 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID{5008A102-08E5-3F59-AADD-03875524CAD0}\InprocServer32\1.0.0.0: 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID{5008A102-08E5-3F59-AADD-03875524CAD0}\ProgId = FirstOutlookAddIn.AddInUtilities
我做错了什么?我是否正确理解这里的可能性,将我的 DLL 加载到 Outlook.exe 中并能够从外部应用程序调用其中的函数? 提前谢谢!!!
【问题讨论】: