DuplicateHandle 伪句柄 与 实句柄的应用
来源:互联网 发布:大学生网络诈骗ppt 编辑:程序博客网 时间:2024/06/05 01:59
如果把GetCurrentThread()返回值传递给一个HANDLE句柄,用它进行ResumeThread,结果肯定不是我们想要的。下面的例子详细描述了伪句柄的调用结果:
#include "stdafx.h" #include <stdio.h> #include <iostream> #include <windows.h> #include <process.h> using namespace std; #pragma warning(disable:4996) HANDLE hThread = NULL; unsigned int __stdcall ProcessInfo(void* lp) { string str = *(string*)lp; delete lp; hThread = GetCurrentThread(); while(true){ SuspendThread(GetCurrentThread()); cout<<str.c_str()<<endl; } _endthreadex(0); return 0; } int _tmain() { string *pStr = new string; *pStr = "老婆, I Love You"; unsigned int dwThreadID; SECURITY_ATTRIBUTES sa; sa.bInheritHandle = FALSE; sa.lpSecurityDescriptor = NULL; sa.nLength = sizeof(SECURITY_ATTRIBUTES); HANDLE hCom = (HANDLE)_beginthreadex(&sa, 0, ProcessInfo, (void*)pStr, 0, &dwThreadID); Sleep(1000);//线程肯定会先执行到SuspendThread,主线程一直在延时,并且全局hThread得到线程的伪句柄ResumeThread(hThread); printf("hThread的句柄值是: %d\n", hThread); Sleep(INFINITE); CloseHandle(hCom); return 0; }
结果显示,hThread是-2,线程没有输出任何东西
修改代码如下:
#include "stdafx.h" #include <stdio.h> #include <iostream> #include <windows.h> #include <process.h> using namespace std; #pragma warning(disable:4996) HANDLE hThread = NULL; unsigned int __stdcall ProcessInfo(void* lp) { string str = *(string*)lp; delete lp; DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hThread, 0, 0, DUPLICATE_SAME_ACCESS); while(true){ SuspendThread(GetCurrentThread()); cout<<str.c_str()<<endl; } _endthreadex(0); return 0; } int _tmain() { string *pStr = new string; *pStr = "老婆, I Love You"; unsigned int dwThreadID; SECURITY_ATTRIBUTES sa; sa.bInheritHandle = FALSE; sa.lpSecurityDescriptor = NULL; sa.nLength = sizeof(SECURITY_ATTRIBUTES); HANDLE hCom = (HANDLE)_beginthreadex(&sa, 0, ProcessInfo, (void*)pStr, 0, &dwThreadID); Sleep(1000);//线程肯定会先执行到SuspendThread,主线程一直在延时,并且全局hThread得到线程的伪句柄ResumeThread(hThread); printf("hThread的句柄值是: %d\n", hThread); Sleep(INFINITE); CloseHandle(hCom); return 0; }
运行正常
0 0
- DuplicateHandle 伪句柄 与 实句柄的应用
- DuplicateHandle伪句柄与实句柄的应用二
- 线程(进程)的句柄与伪句柄
- DuplicateHandle()复制句柄函数的用法
- 实句柄和伪句柄
- 实句柄和伪句柄
- 实句柄和伪句柄
- DuplicateHandle复制句柄保护文件
- DuplicateHandle进程间句柄复制
- 伪句柄 pseudohandle 和句柄的转换
- [转载]实句柄和伪句柄
- 句柄跟伪句柄
- 句柄和伪句柄
- 句柄和伪句柄
- 伪句柄
- 伪句柄
- 伪句柄
- 伪句柄
- 滤波的概念和作用(滤波器、掩模、核、模板、窗口是一个意思)
- ora-02085的解决:Database Link与GLOBAL_NAMES参数
- JavaScript浏览器兼容问题
- 前端基础
- GitHub开源组件集锦
- DuplicateHandle 伪句柄 与 实句柄的应用
- Hello Giraph
- TextBox多行文本框内容自动换行
- 判断一个图片是不是伪造的方法
- AngularJS: Factory vs Service vs Provider
- Android笔记ContentUris类使用介绍
- ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句
- Ubuntu 14.04 samba安装
- 设置全选按钮功能