将IntPtr值转换为Int32 [英] Converting IntPtr value to Int32
问题描述
亲爱的所有人,
我正在尝试将IntPtr值转换为Int32,但是代码抛出异常:
无法将类型为"System.IntPtr"的对象强制转换为类型为"System.IConvertible"的对象"
在线:
点p =新的Point(LoWord(Convert.ToInt32(lparam)),HiWord(Convert.ToInt32(lparam)));"
请帮助解决问题.
提前谢谢您.
IB
IntPtr具有ToInt32()和ToInt64()方法.
IntPtr ip =( IntPtr ) int i = Convert.ToInt32(ip); // 将给出运行时异常. int j =( int )ip; // 会工作
public static explicit operator int ( IntPtr value );
http://msdn.microsoft.com/en-us/library/9a8d37fb.aspx [ ^ ]
那应该做.
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
IntPtr has the methods ToInt32() and ToInt64().
Cast it instead of trying to Convert it:
IntPtr ip = (IntPtr) 123; int i = Convert.ToInt32(ip); //Will give a run time exception. int j = (int)ip; //Will work
public static explicit operator int ( IntPtr value );
http://msdn.microsoft.com/en-us/library/9a8d37fb.aspx[^]
That should do it.
这篇关于将IntPtr值转换为Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!