Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am trying to convert IntPtr value to Int32 but the code is throwing exception:

"Unable to cast object of type 'System.IntPtr' to type 'System.IConvertible'"

at line:

"Point p = new Point(LoWord(Convert.ToInt32(lparam)), HiWord(Convert.ToInt32(lparam)));"


Kindly help resolve the problem.

Thanking you in advance.

IB
Posted

IntPtr has the methods ToInt32() and ToInt64().
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Mar-12 21:36pm    
My 5. Also, it's important to check for 64-bit instruction-set architecture. (See System.Environment.)
--SA
Cast it instead of trying to Convert it:
C#
IntPtr ip = (IntPtr) 123;
int i = Convert.ToInt32(ip);  //Will give a run time exception.
int j = (int)ip;              //Will work
 
Share this answer
 
Comments
Indrojeet_Bhattacharya 30-Mar-12 16:09pm    
Thank you very much. The solution worked.
OriginalGriff 31-Mar-12 3:32am    
You're welcome!
public static explicit operator int (
	IntPtr value
);

http://msdn.microsoft.com/en-us/library/9a8d37fb.aspx[^]
That should do it.
 
Share this answer
 
v2
Comments
pietvredeveld 30-Mar-12 16:03pm    
Nice solution

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