当使用(uint)-1时,编译器会给出警告:常量-1无法转换为uint,使用unchecked语句重写。DWORD在转换为C#类型时为uint,既然无法使用uint强制转型(-1),那就需要其他办法了。既然编译器给出的提示是使用unchecked语句重写,可以一试。之前没有使用过unchecked语句,所以不熟悉其用法。看了下参考文档,MSDN是这样描述unchecked的:

The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.
In an unchecked context, if an expression produces a value that is outside the range of the destination type, the overflow is not flagged. For example,
because the calculation in the following example is performed in an unchecked block or expression, the fact that the result is too large for an integer
is ignored, and int1 is assigned the value -2,147,483,639.

简单翻译一下,其意思是:

unchecked 关键字用于禁止编译器对整型算术运算和转型进行溢出检查。
在unchecked上下中,如果一个表达式产生的值超过了目标类型的值范围,那么这个溢出不会被标记报告。举一个例子,在接下来的例子中演示了一个unchecked块,计算的结果已经超过了整型的最大值......
(就是MAX INT + 10, 结果超过了最大的整型能表达的值。在计算机中,溢出会造成符号位进位或者取反)

所以当遇到Win32 Api中这样的函数定义以及参数说明时,可以使用unchecked 来传入 -1 .

//dwPrefMaxLen [in]
//A value that specifies the preferred maximum length of returned data, in //8-bit bytes. If this parameter is -1, the buffer that is returned is large //enough to hold all available data. uint preMax;
unchecked
{
preMax = (uint)-;
}

参考:

[1]unchecked, https://msdn.microsoft.com/en-us/library/a569z7k8.aspx

最新文章

  1. iOS 有TabBar的VC界面push后隐藏TabBar的方法
  2. ANSI Common Lisp Practice - My Answers - Chatper - 2
  3. 使用xtrabackup备份mysql数据库
  4. 漫谈可视化Prefuse(六)---改动源码定制边粗细
  5. Debian8修改启动默认运行级别
  6. Windows 服务器使用FTP出现“当前的安全设置不允许从该位置下载文件" 警告
  7. Python搭建简易HTTP服务(3.x版本和2.x版本的)
  8. ASP多行多列显示代码
  9. 二、 What's Maven,How to learning?
  10. ios xcode如何在控制台打印frame
  11. iOS开发:UILabel无法响应点击事件的问题
  12. linux 命令后台执行
  13. Python-----格式化字符
  14. 《精通Spring 4.X企业应用开发实战》读书笔记1-1(IoC容器和Bean)
  15. 563. Binary Tree Tilt
  16. mysql5.5.28在Linux下的安装
  17. Android Activity学习笔记——Activity的启动和创建
  18. Centos7 -- glibc 升级失败、意外删除、故意删除后的处理方法
  19. 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
  20. lua --- 用break实现continue逻辑

热门文章

  1. 定义Portal显示规则
  2. SQL Server 2012内部原理及故障排除
  3. VS2017 +NetCore2.2.0+WebApi项目整合SwaggerUI 以及遇到的坑
  4. ubuntu 搭建 svn服务器,使用http方式访问
  5. express 不是内部或外部命令(windows)解决方式
  6. using gdb to debug c program
  7. $.getJSON() 未能执行回调函数的缘由
  8. LeetCode 246. Strobogrammatic Number (可颠倒数字) $
  9. Cant't call setState(or forceUpdate) on an unmount component. 报错的可能性原因
  10. 又发现2个高人写的Delphi图文并茂的消息研究