Click here to Skip to main content
13,353,322 members (51,692 online)
Rate this:
Please Sign up or sign in to vote.
See more:
My C# (asp) code is :

int real_buffer_size=0;
				// allocate a dump memory space in order to retrieve nb of connexion
				int BufferSize = 100*CMIB_TCPEXROW.size_ex+4;//NumEntries*CMIB_TCPEXROW.size_ex+4
				IntPtr lpTable = Marshal.AllocHGlobal(BufferSize);
				//get the number of entries in the table
Error Line==>	Int32 NumEntries= (int)Marshal.ReadIntPtr(lpTable);

				// check if memory was enougth (needed buffer size: NumEntries*MIB_UDPEXROW.size_ex +4 (for dwNumEntries))
				if (BufferSize<real_buffer_size)
					// free the buffer
					// get the needed buffer size: NumEntries*MIB_TCPEXROW.size_ex +4 (for dwNumEntries)
					BufferSize = real_buffer_size;
					// Allocate memory
					lpTable = Marshal.AllocHGlobal(BufferSize);

when I run my C# code (ASP) it stop with error :
OverflowException occurred
Arithmetic operation resulted in an overflow.

in line:
Int32 NumEntries= (int)Marshal.ReadIntPtr(lpTable);

I know what the Error mean, but I do not know how to resolve it! which changes should I make to fix this error? (for example, which parameter should I change?)

Your Help will be Appreciated,
Posted 16-Jan-13 5:43am
Updated 10-Apr-16 8:33am
Nick Fisher (Consultant) 16-Jan-13 10:46am
Which line does it fail on? A bit more info will help to get the best response.
Mathlab 16-Jan-13 11:56am
1. Breakpoint
2. Watch your locals window
3. Increase the size of variable overflowing
4. Change the type to var of the variable overflowing and do not cast it to int
5. Start at 1. if it fails
Richard MacCutchan 16-Jan-13 12:56pm
I suspect the actual error occurs on the following line where you are multiplying two values together. Add some debug code to see what the two values are.
jkirkerx 16-Jan-13 18:44pm
case from int to int32, I've had trouble with that in the past
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

TL;DR: You're casting 64-bits IntPtr to a Int32... Which causes "overflow"

At first, I thought it's most likely your '+4', you're assuming that the padding is 4 bytes wide. Unless you've explicitly compiled as platform targeted for x86 (and not x86_64 or "Any CPU"), your "lp" pointer size (of your buffer you're iterating) would be 64-bits (on "Any CPU", it flips between O/S running 32-bit or 64-bits). Change your +4 to be more dynamic (+4 for 32-bit, +8 for 64-bits) in whatever techniques you chose (i.e. "... + IntPtr.Size" instead of "... + 4").

On that note...

But then, I've realized that you're getting a casting exception in which you're attempting to cast a 64-bits pointer (IntPtr) into an "Int" (and then, you assign it to Int32, though "int" is alias to System.Int32 from I remember). It works on your 32-bits O/S or when you explicitly compile/build as "x86" but when you do "Any CPU" or 64-bits mode, you get exceptions during runtime.

Good luck && peace!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This looks like horrible code to run in a web page. What is it doing ? You are doing a cast, what does ReadIntPtr return ? I suggest stuffing your return value in to the right type, without casting. Then you will know for sure if the call itself is failing, or the cast. Are you sure that lpTable has a good value in it ? Have you debugged this ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Go to Application pool=>Advance settings and enable 32 bit application to true. It resolved it for me.
CHill60 10-Apr-16 14:20pm
Question was answered a year ago. Somehow I doubt that this would be an appropriate solution in this world of 64-bits

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 10 Apr 2016
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100