VC中易犯的错误

在这里面总结一下VC中易常犯的错误.
一/前两天过一个串口的通迅程序,有如下一段代码:
HANDLE hCom=CreateFile(......);(构造函数里面将hCom=NULL)
if(hCom)
{
//如果hCom有值,则说明创建成功
}
else
{
//没成功
}
看上去好像没什么问题,其实错的很彻底.
Windows via c/c++中有这样一段话:

If CreateFile succeeds in creating or opening a file or device, the handle of the file or device is returned. If CreateFile fails, INVALID_HANDLE_VALUE is returned.


Note 

Most Windows functions that return a handle return NULL when the function fails. However, CreateFile returns INVALID_HANDLE_VALUE (defined as –1) instead. I have often seen code like this, which is incorrect:

HANDLE hFile = CreateFile(...);
if (hFile == NULL) {
// We'll never get in here
} else {
// File might or might not be created OK
}

Here's the correct way to check for an invalid file handle:

HANDLE hFile = CreateFile(...);
if (hFile == INVALID_HANDLE_VALUE) {
// File not created
} else {
// File created OK
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值