Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My C# (asp) code is :

C#
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);
				real_buffer_size=NumEntries*CMIB_TCPEXROW.size_ex+4;

				// check if memory was enougth (needed buffer size: NumEntries*MIB_UDPEXROW.size_ex +4 (for dwNumEntries))
				if (BufferSize<real_buffer_size)
				{
					// free the buffer
					Marshal.FreeHGlobal(lpTable);
	                
					// 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 :
Quote:
OverflowException occurred
Arithmetic operation resulted in an overflow.


in line:
C#
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
Updated 10-Apr-16 7:33am
v2
Comments
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.
[no name] 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

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!
 
Share this answer
 
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 ?
 
Share this answer
 
Go to Application pool=>Advance settings and enable 32 bit application to true. It resolved it for me.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900