gpt4 book ai didi

c# - Outlook VSTO 加载项与外部 C++ 应用程序之间的交互

转载 作者:行者123 更新时间:2023-11-30 03:21:00 26 4
gpt4 key购买 nike

我有一个 C++ 应用程序,我正在编写一个新的 Outlook 加载项,我认为它会与 VSTO 一起使用。我想在那之间进行交流,并且我正在尝试找出最好的方法。关于MS docs他们提到如何使用 RequestComAddInAutomationService 将您的 COM 类暴露给外部解决方案。我是 COM 的新手,但我在网上阅读了一些内容并找到了以下解决方案。我读到你应该构建插件(x86 作为我的 Outlook 版本而不是 AnyCPU),获取创建的 .tlb 文件并将其转换为 .tlh 使用 #import 指令,然后使用 #include .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 类未注册。相关的注册表树如下所示:

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: Computer\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: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID{5008A102-08E5-3F59-AADD-03875524CAD0}\InprocServer32\1.0.0.0Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID{5008A102-08E5-3F59-AADD-03875524CAD0}\ProgId = FirstOutlookAddIn.AddInUtilities

我做错了什么?我是否正确理解了这里的可能性,将我的 DLL 加载到 Outlook.exe 中并能够从外部应用程序调用其中的函数?提前致谢!!!

最佳答案

您不应该从您的 C++ 应用程序创建该 COM 类的实例 - 创建 Outlook.Application 对象的实例,使用 Application.COMAddins 集合来获取您的插件,然后使用 COMAddin.Object 属性检索您的插件实现的接口(interface)。
参见,例如,https://blogs.msdn.microsoft.com/andreww/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice/

关于c# - Outlook VSTO 加载项与外部 C++ 应用程序之间的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52398438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com