C# 将System.IntPtr强制转换到uint时发生System.OverflowException
我试图将C# 将System.IntPtr强制转换到uint时发生System.OverflowException,c#,overflowexception,C#,Overflowexception,我试图将SystemInfo中的MaximumApplicationAddress保存到uint中,但出现系统溢出异常: 系统。溢出异常:算术运算导致溢出 我尝试了很多,也用谷歌搜索了很多,但没有任何帮助。如果我转换或使用十进制,没有任何帮助 下面是有问题的代码: private uint _maxAddress; public MemorySearch() { SystemInfo info; GetSystemInfo(out info); // This lin
SystemInfo
中的MaximumApplicationAddress
保存到uint
中,但出现系统溢出异常
:
系统。溢出异常:算术运算导致溢出
我尝试了很多,也用谷歌搜索了很多,但没有任何帮助。如果我转换或使用十进制,没有任何帮助
下面是有问题的代码:
private uint _maxAddress;
public MemorySearch()
{
SystemInfo info;
GetSystemInfo(out info);
// This line throws a System.OverflowException:
_maxAddress = (uint)info.MaximumApplicationAddress;
resetFields();
}
以下是错误的屏幕截图:
有什么想法吗?编译器告诉您
最大应用程序地址的大小大于uint
请尝试改用长
(64位整数):
没有从IntPtr
到uint
(UInt32
)的显式或隐式转换。显式转换为Int32
。看
如果在64位系统上调用该函数,则该函数将抛出OverflowException
如果您真的想将64位指针转换为32位值,则必须先转换为ulong
,然后转换为uint
,发布源代码截图通常是不好的做法。变量监视窗格显示MaximumApplicationAddress
的值为0x00007ffffffff
(在base-10中为140737488289791
)。这超出了。存储在中的值的范围。
private long _maxAddress;
public MemorySearch()
{
SystemInfo info;
GetSystemInfo(out info);
_maxAddress = info.MaximumApplicationAddress.ToInt64();
resetFields();
}