C# MAPI在一个x64系统上工作,但在另一个系统上不工作
我想通过MAPI发送电子邮件。在一台Windows服务器2008 R2 x64上,它工作正常。我在Windows Server 2012 R2 x64上尝试了相同的代码,但始终出现以下异常: System.OverflowException:算术运算导致溢出。 在System.IntPtr.op_显式(IntPtr值) 代码如下所示:C# MAPI在一个x64系统上工作,但在另一个系统上不工作,c#,mapi,C#,Mapi,我想通过MAPI发送电子邮件。在一台Windows服务器2008 R2 x64上,它工作正常。我在Windows Server 2012 R2 x64上尝试了相同的代码,但始终出现以下异常: System.OverflowException:算术运算导致溢出。 在System.IntPtr.op_显式(IntPtr值) 代码如下所示: IntPtr GetRecipients(out int recipCount) { recipCount = 0; if (m_recipien
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_收件人。计数=1
为什么它只能在一个系统上工作,而不能在另一个系统上工作?经过一些额外的调试后,我发现Hans Passant是正确的。问题所在是: intPtr=(int)intPtr 我不得不改成
long
,结果成功了
long ptr=(long)intPtr
不确定您是否识别了正确的语句,MarshallAlloc调用没有IntPtr转换。我需要异常堆栈跟踪来进行调用。但是从int到int的转换肯定是不安全的。这很容易在64位操作系统上爆炸。一个32位的程序可以有4G的地址空间,一个64位的程序将在最新的Windows版本中分配高地址。改为使用IntPtr.Add();我不得不把它改成long,它成功了。