编程语言
首页 > 编程语言> > c# – Office VSTO加载项可能的权限问题 – HRESULT 0x80004004(E_ABORT)

c# – Office VSTO加载项可能的权限问题 – HRESULT 0x80004004(E_ABORT)

作者:互联网

我们开发了一个C#Office VSTO加载项,它与正在运行的Outlook实例进行通信(或启动一个新实例),并且在尝试创建Outlook任务或约会时,它显示出某些客户PC上存在权限问题的迹象. .

异常消息如下:

Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))

这发生在这里:

Outlook.Account DefaultAccount = null;
Outlook.Application outlookApp = GetOutlookApp();    //returns Application object of running Outlook instance / creates a new instance - it works for them.

DefaultAccount = GetAccountForFolder(outlookApp);    //returns the default account of the user. Tried it with a simple setup, only one account etc. - it works for them
String defaultemailaddress;

//CODE RUNS UNTIL THIS POINT
if (DefaultAccount == null)    //if somehow this would end up NULL, which is not the case, because: see code snippet below!
{
    defaultemailaddress = outlookApp.Session.CurrentUser.AddressEntry.Address;
}
else
{
    defaultemailaddress = DefaultAccount.SmtpAddress;    //this could be the problem, but I can't debug it further, and it works in the code block below, to get the AccountType, so I don't understand why I couldn't get the SmtpAddress without a hard exception
}
//FAILS BEFORE THIS LINE COULD RUN.
String email = "test@emailserver.com";

在与用户取得联系后,他们告诉我们,他们是在一个非常有限的权限集和网络下运行的.

奇怪的是,这段代码实际上运行顺利,证明了Outlook与其他Office加载项之间的连接正常:

Outlook.Application oApp = GetOutlookApp();
Outlook.Account DefaultAccount = GetAccountForFolder(oApp);
String AccountType = DefaultAccount.AccountType.ToString();

IT部门已经尝试在受影响的PC上调整Outlook的安全策略.他们允许程序化访问.

他们无法使用管理员权限启动工具,但这不是必需的.最后3行代码工作(获取帐户类型)的事实证明应用程序确实正确启动,但看起来它只能运行某些功能……

我还想注意,他们正在使用Exchange,但显然他们没有同步问题(如果这些可能影响任何事情,那么……)

编辑:
这是GetAccountForFolder的实现,它获取默认的Outlook.Account对象.这是我发现的代码片段,发现它工作得很好.

public static Outlook.Account GetAccountForFolder(Outlook.Application outlookApp)
{
    // Obtain the store on which the folder resides.
    Outlook.Store store = outlookApp.Session.DefaultStore;

    // Enumerate the accounts defined for the session.
    foreach (Outlook.Account account in outlookApp.Session.Accounts)
    {
        // Match the DefaultStore.StoreID of the account
        // with the Store.StoreID for the currect folder.
        if (account.DeliveryStore.StoreID == store.StoreID)
        {
            // Return the account whose default delivery store
            // matches the store of the given folder.
            return account;
        }
    }
    // No account matches, so return null.
    return null;
}

解决方法:

问题是你有race condition in a COM Add-In object.你应该在你的方法中做一个人为的延迟.简单地重试,直到你成功,像这样:

bool isDone = false;
while (!isDone)
{
    try
    {
        // your action with Add-In here...

        isDone = true;
    }
    catch (System.Runtime.InteropServices.COMException exception)
    {
        // small delay
        Thread.Sleep(10);
    }
}

标签:c,vsto,outlook,outlook-addin,office-addins
来源: https://codeday.me/bug/20190705/1390659.html