【问题标题】:MAPI works on one x64 System but not anotherMAPI 适用于一个 x64 系统,但不适用于另一个
【发布时间】:2018-11-19 15:48:41
【问题描述】:

我想通过 MAPI 发送电子邮件。在一台 Windows Server 2008 R2 x64 上运行良好。我在 Windows Server 2012 R2 x64 上尝试了相同的代码,我总是得到以下异常:

System.OverflowException:算术运算导致溢出。 在 System.IntPtr.op_Explicit(IntPtr 值)

代码如下所示:

IntPtr GetRecipients(out int recipCount)
{
    recipCount = 0;
    if (m_recipients.Count == 0)
        return IntPtr.Zero;

    int size = Marshal.SizeOf(typeof(MapiRecipDesc));
    IntPtr intPtr = Marshal.AllocHGlobal(m_recipients.Count * size);

    int ptr = (int)intPtr;
    foreach (MapiRecipDesc mapiDesc in m_recipients)
    {
        Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);
        ptr += size;
    }

    recipCount = m_recipients.Count;
    return intPtr;
}

这一行:

IntPtr intPtr = Marshal.AllocHGlobal(m_recipients.Count * size);

是错误发生的地方。

size = 40 和 m_recipients.Count = 1。

为什么它可以在一个系统上运行,而不能在另一个系统上运行?

【问题讨论】:

  • 不确定您是否确定了正确的语句,MarshalAlloc 调用没有 IntPtr 转换。我需要异常堆栈跟踪才能进行调用。但是到/从 int 的强制转换肯定是不安全的。这很容易轰炸 64 位操作系统。一个 32 位程序可以有一个 4 GB 的地址空间,一个 64 位程序将在最近的 Windows 版本上分配高地址。请改用 IntPtr.Add()。
  • 你说得对,问题在于 int ptr = (int)intPtr;我不得不把它改成 long 并且它起作用了。

标签: c# mapi


【解决方案1】:

经过一些额外的调试,我发现 Hans Passant 是对的。问题所在的行是:

int ptr = (int)intPtr;

我不得不更改为 long 并且成功了

long ptr = (long)intPtr;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2019-11-22
    • 2012-03-22
    • 2012-02-26
    相关资源
    最近更新 更多