MAPI 适用于一个 x64 系统,但不适用于另一个

MAPI works on one x64 System but not another

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

System.OverflowException: Arithmetic operation resulted in an overflow. at System.IntPtr.op_Explicit(IntPtr value)

代码如下所示:

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);

是错误发生的地方。

大小 = 40 和 m_recipients.Count = 1。

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

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

int ptr = (int)intPtr;

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

long ptr = (long)intPtr;