WINDOWS HANDLE null INVALID_HANDLE_VALUE
来源:互联网 发布:lnmp 查看php日志 编辑:程序博客网 时间:2024/06/04 18:30
1 not null
2 not null
因此CloseHandle后不能用 if ( handle )
- void handle_null_test()
- {
- HANDLE hevent = CreateEvent(NULL, FALSE, FALSE, L"xx");
- if ( hevent )
- {
- printf("1 not null\n");
- }
- CloseHandle(hevent);
- if ( hevent )
- {
- printf("2 not null\n");
- }
- }
Windows API,可能会返回NULL或者INVALID_HANDLE_VALUE,具体要看MSDN。原因见下面
PS:
http://blogs.msdn.com/b/oldnewthing/archive/2004/03/02/82639.aspx
If you look at the various functions that return HANDLEs,you'll see that some of them return NULL (like CreateThread)and some of them return INVALID_HANDLE_VALUE (like CreateFile).You have to check the documentation to see what each particular functionreturns on failure.
Why are the return values so inconsistent?
The reasons, as you may suspect, are historical.
The values were chosen to be compatible with 16-bit Windows.The 16-bit functions OpenFile, _lopen and _lcreat return -1 on failure, so the 32-bit CreateFile function returns INVALID_HANDLE_VALUE in order to facilitate porting code from Win16.
(Armed with this, you can now answer the following triviaquestion: Why do I call CreateFile when I'm not actually creating a file? Shouldn't it be called OpenFile? Answer: Yes, OpenFile would havebeen a better name, but that name was already taken.)
On the other hand, there are no Win16 equivalents for CreateThread or CreateMutex, so theyreturn NULL.
Since the precedent had now been set for inconsistent return values,whenever a new function got added, it was a bit of a toss-up whetherthe new function returned NULL or INVALID_HANDLE_VALUE.
This inconsistency has multiple consequences.
First, of course, you have to be careful to check the return valuesproperly.
Second, it means that if you write a generic handle-wrapping class,you have to be mindful of two possible "not a handle" values.
Third, if you want to pre-initialize a HANDLE variable,you have to initialize it in a manner compatible with the functionyou intend to use. For example, the following code is wrong:
HANDLE h = NULL;if (UseLogFile()) { h = CreateFile(...);}DoOtherStuff();if (h) { Log(h);}DoOtherStuff();if (h) { CloseHandle(h);}This code has two bugs. First, the return value from CreateFile is checked incorrectly. The code abovechecks for NULL instead of INVALID_HANDLE_VALUE.Second, the code initializes the h variable incorrectly.Here's the corrected version:
HANDLE h = INVALID_HANDLE_VALUE;if (UseLogFile()) { h = CreateFile(...);}DoOtherStuff();if (h != INVALID_HANDLE_VALUE) { Log(h);}DoOtherStuff();if (h != INVALID_HANDLE_VALUE) { CloseHandle(h);}
Fourth, you have to be particularly careful with the INVALID_HANDLE_VALUE value:By coincidence, the value INVALID_HANDLE_VALUE happens to be numerically equal to the pseudohandle returned by GetCurrentProcess().Many kernel functions accept pseudohandles, so ifif you mess upand accidentally call, say, WaitForSingleObject on afailed INVALID_HANDLE_VALUE handle, you will actuallyend up waiting on your own process. This wait will, of course,never complete, because a process is signalled when it exits,so you ended up waiting for yourself.
上一篇:Windows SetEvent OpenEvent返回183
下一篇:工作小结
- LNK1123: 转换到 COFF 期间失...
- WIN7访问共享:0x80070035 找不...
- Delphi 2010下载+完美破解...
- vs2010调试C++程序时提示 无...
- VISIO,不规则封闭图形填充方...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
- WINDOWS HANDLE null INVALID_HANDLE_VALUE
- HANDLE: INVALID_HANDLE_VALUE vs NULL
- HANDLE的无效值:NULL还是INVALID_HANDLE_VALUE? 以及对HANDLE的RAII封装
- 返回HANDLE的API,失败时有时候是NULL,有时候是INVALID_HANDLE_VALUE
- INVALID_HANDLE_VALUE 与 NULL
- Windows内核对象无效返回值——NULL或者INVALID_HANDLE_VALUE??
- Windows中的句柄(handle)
- windows handle之DuplicateHandle
- windows handle msg
- windows handle manager
- Windows HANDLE是什么
- Unable to handle kernel NULL pointer dereference.
- Unable to handle kernel NULL pointer dereference.
- Windbg调试windows handle泄漏
- FireMonkey的窗口Handle转为Windows窗口的Handle
- FireMonkey的窗口Handle转为Windows窗口的Handle
- 介绍Unable to handle kernel NULL pointer dereference...
- unable to handle kernel null pointer dereference at virtual address
- 改变字符串中指定字符的颜色
- Windows服务处于已停止
- 示例八和示例九
- Windows SetEvent OpenEvent返回183
- Jetty的安装和配置
- WINDOWS HANDLE null INVALID_HANDLE_VALUE
- 无法与域的active directory域控制器连接
- Windows 添加域用户
- 禁用虚拟机中开始菜单Shutdown中的断开连接,注销, 切换用户,锁定等选项
- 控制评价小星星不能拖拽
- VS2010 C++ 高亮
- AVRO 数据序列化系统学习笔记
- android性能优化之代码优化
- 男生如何找准自己的穿衣风格,提升衣着品味(转载:搜狐男人)