MAPI works on one x64 System but not another












2















I want to send E-Mails via MAPI. On one Windows Server 2008 R2 x64 it works fine. I tried the same Code on a Windows Server 2012 R2 x64 there I always get the following Exception:




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




The Code looks like this:



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


This line:




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




is where the Error occurs.



size = 40 and m_recipients.Count = 1.



Why does it work on one System but not another?










share|improve this question




















  • 2





    Not so sure you identified the correct statement, the MarshalAlloc call has no IntPtr cast. I'd need the exception stack trace to make the call. But the casts to/from int are certainly not safe. That can easily bomb on a 64-bit operating system. A 32-bit program can have a 4 gigabyte address space, a 64-bit program will allocate at high addresses on recent Windows versions. Use IntPtr.Add() instead.

    – Hans Passant
    Nov 19 '18 at 17:49













  • You're right the problem was the line int ptr = (int)intPtr; I had to change it to long and it worked.

    – E-Nuff
    Nov 20 '18 at 7:27


















2















I want to send E-Mails via MAPI. On one Windows Server 2008 R2 x64 it works fine. I tried the same Code on a Windows Server 2012 R2 x64 there I always get the following Exception:




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




The Code looks like this:



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


This line:




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




is where the Error occurs.



size = 40 and m_recipients.Count = 1.



Why does it work on one System but not another?










share|improve this question




















  • 2





    Not so sure you identified the correct statement, the MarshalAlloc call has no IntPtr cast. I'd need the exception stack trace to make the call. But the casts to/from int are certainly not safe. That can easily bomb on a 64-bit operating system. A 32-bit program can have a 4 gigabyte address space, a 64-bit program will allocate at high addresses on recent Windows versions. Use IntPtr.Add() instead.

    – Hans Passant
    Nov 19 '18 at 17:49













  • You're right the problem was the line int ptr = (int)intPtr; I had to change it to long and it worked.

    – E-Nuff
    Nov 20 '18 at 7:27
















2












2








2








I want to send E-Mails via MAPI. On one Windows Server 2008 R2 x64 it works fine. I tried the same Code on a Windows Server 2012 R2 x64 there I always get the following Exception:




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




The Code looks like this:



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


This line:




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




is where the Error occurs.



size = 40 and m_recipients.Count = 1.



Why does it work on one System but not another?










share|improve this question
















I want to send E-Mails via MAPI. On one Windows Server 2008 R2 x64 it works fine. I tried the same Code on a Windows Server 2012 R2 x64 there I always get the following Exception:




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




The Code looks like this:



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


This line:




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




is where the Error occurs.



size = 40 and m_recipients.Count = 1.



Why does it work on one System but not another?







c# mapi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 15:54







E-Nuff

















asked Nov 19 '18 at 15:48









E-NuffE-Nuff

10716




10716








  • 2





    Not so sure you identified the correct statement, the MarshalAlloc call has no IntPtr cast. I'd need the exception stack trace to make the call. But the casts to/from int are certainly not safe. That can easily bomb on a 64-bit operating system. A 32-bit program can have a 4 gigabyte address space, a 64-bit program will allocate at high addresses on recent Windows versions. Use IntPtr.Add() instead.

    – Hans Passant
    Nov 19 '18 at 17:49













  • You're right the problem was the line int ptr = (int)intPtr; I had to change it to long and it worked.

    – E-Nuff
    Nov 20 '18 at 7:27
















  • 2





    Not so sure you identified the correct statement, the MarshalAlloc call has no IntPtr cast. I'd need the exception stack trace to make the call. But the casts to/from int are certainly not safe. That can easily bomb on a 64-bit operating system. A 32-bit program can have a 4 gigabyte address space, a 64-bit program will allocate at high addresses on recent Windows versions. Use IntPtr.Add() instead.

    – Hans Passant
    Nov 19 '18 at 17:49













  • You're right the problem was the line int ptr = (int)intPtr; I had to change it to long and it worked.

    – E-Nuff
    Nov 20 '18 at 7:27










2




2





Not so sure you identified the correct statement, the MarshalAlloc call has no IntPtr cast. I'd need the exception stack trace to make the call. But the casts to/from int are certainly not safe. That can easily bomb on a 64-bit operating system. A 32-bit program can have a 4 gigabyte address space, a 64-bit program will allocate at high addresses on recent Windows versions. Use IntPtr.Add() instead.

– Hans Passant
Nov 19 '18 at 17:49







Not so sure you identified the correct statement, the MarshalAlloc call has no IntPtr cast. I'd need the exception stack trace to make the call. But the casts to/from int are certainly not safe. That can easily bomb on a 64-bit operating system. A 32-bit program can have a 4 gigabyte address space, a 64-bit program will allocate at high addresses on recent Windows versions. Use IntPtr.Add() instead.

– Hans Passant
Nov 19 '18 at 17:49















You're right the problem was the line int ptr = (int)intPtr; I had to change it to long and it worked.

– E-Nuff
Nov 20 '18 at 7:27







You're right the problem was the line int ptr = (int)intPtr; I had to change it to long and it worked.

– E-Nuff
Nov 20 '18 at 7:27














1 Answer
1






active

oldest

votes


















1














After some additional debugging I saw that Hans Passant was right. The line that was the problem was:




int ptr = (int)intPtr;




I had to change to long and it worked




long ptr = (long)intPtr;







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378217%2fmapi-works-on-one-x64-system-but-not-another%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    After some additional debugging I saw that Hans Passant was right. The line that was the problem was:




    int ptr = (int)intPtr;




    I had to change to long and it worked




    long ptr = (long)intPtr;







    share|improve this answer




























      1














      After some additional debugging I saw that Hans Passant was right. The line that was the problem was:




      int ptr = (int)intPtr;




      I had to change to long and it worked




      long ptr = (long)intPtr;







      share|improve this answer


























        1












        1








        1







        After some additional debugging I saw that Hans Passant was right. The line that was the problem was:




        int ptr = (int)intPtr;




        I had to change to long and it worked




        long ptr = (long)intPtr;







        share|improve this answer













        After some additional debugging I saw that Hans Passant was right. The line that was the problem was:




        int ptr = (int)intPtr;




        I had to change to long and it worked




        long ptr = (long)intPtr;








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 8:00









        E-NuffE-Nuff

        10716




        10716






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378217%2fmapi-works-on-one-x64-system-but-not-another%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents