博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Win API:之GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId
阅读量:7244 次
发布时间:2019-06-29

本文共 3055 字,大约阅读时间需要 10 分钟。

  Win API:之GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId

 {返回当前线程的虚拟句柄} GetCurrentThread: THandle; {返回当前线程 ID} GetCurrentThreadId: DWORD; {返回当前进程的虚拟句柄} GetCurrentProcess: THandle; {返回当前进程 ID} GetCurrentProcessId: DWORD;

  提示:

  ID 是系统唯一的标识.
  所谓虚拟句柄, 就是该句柄只在调用进程的进程中有效, 也不能被继承;
  如果用于其他进程需要用 DuplicateHandle 复制句柄;
  GetCurrentProcess 返回的虚拟句柄可以通过 OpenProcess 创建一个真实的句柄.

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
#include
 <iostream>
#include
 <Windows.h>
#include
 <process.h>
#include
 <tchar.h>
#include
 <strsafe.h>
#define
 BUF_SIZE 
255
typedef
 
struct
 MyData {
    
int
 nVal1;
    
int
 nVal2;
} MYDATA, *PMYDATA;
using
 
namespace
 std;
DWORD WINAPI MyThread( LPVOID lpParam );
int
 main(
void
)
{
    HANDLE hProcess = 
NULL
;
    DWORD dwProcessId = 
0
;
    HANDLE hThread = 
NULL
;
    DWORD dwThreadId = 
0
;
    PMYDATA pData;
    hProcess = GetCurrentProcess(); 
//进程伪句柄
    cout << 
"The Current Process Pseudo Handle is "
 << hProcess << endl;
    DuplicateHandle(GetCurrentProcess(),
        GetCurrentProcess(),
        GetCurrentProcess(),
        &hProcess,
        
0
,
        FALSE,
        DUPLICATE_SAME_ACCESS
        );
    cout << 
"The Current Process Real Handle is "
 << hProcess << endl;
    dwProcessId = GetCurrentProcessId();
    cout << 
"The Current Process Id is "
 << dwProcessId << endl;
    
    
// Allocate memory for thread data.
    pData = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
sizeof
(MYDATA));
    
if
( pData == 
NULL
 )
    {
        ExitProcess(
2
);
    }
    
// Generate unique data for each thread.
    pData->nVal1 = 
1
;
    pData->nVal2 = 
100
;
    
// Create Thread
    hThread = CreateThread( 
NULL
,               
// default security attributes
                            
0
,                  
// use default stack size  
                            MyThread,           
// thread function 
                            pData,              
// argument to thread function 
                            
0
,                  
// use default creation flags 
                            &dwThreadId);       
// returns the thread identifier 
    cin.get();
    
return
 
0
;
}
DWORD WINAPI MyThread( LPVOID lpParam )
{
    HANDLE hThread = 
NULL
;
    DWORD dwThreadId = 
0
;
    hThread = GetCurrentThread();   
//线程伪句柄
    cout << 
"The Current Thread Pseudo Handle is "
 << hThread << endl;
    DuplicateHandle(GetCurrentProcess(),
        GetCurrentThread(),
        GetCurrentProcess(),
        &hThread,
        
0
,
        FALSE,
        DUPLICATE_SAME_ACCESS
        );
    cout << 
"The Current Thread Real Handle is "
 << hThread << endl;
    dwThreadId = GetCurrentThreadId();
    cout << 
"The Current Thread Id is "
 << dwThreadId << endl;
    HANDLE hStdout;
    PMYDATA pData;
    TCHAR msgBuf[BUF_SIZE] = {
0
};
    size_t cchStringSize;
    DWORD dwChars;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    
if
( hStdout == INVALID_HANDLE_VALUE )
        
return
 
1
;
    
// Cast the parameter to the correct data type.
    pData = (PMYDATA)lpParam;
    
// Print the parameter values using thread-safe functions.
    StringCchPrintf(msgBuf, 
                    BUF_SIZE, 
                    TEXT(
"Parameters = %d, %d\n"
), 
                    pData->nVal1, 
                    pData->nVal2); 
    StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
    WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, 
NULL
);
    
return
 
0
}

转载地址:http://gxybm.baihongyu.com/

你可能感兴趣的文章
基础002_V7-CLB
查看>>
Joseph UVA 1452 Jump
查看>>
学习进度
查看>>
浅析JavaScript事件流——冒泡
查看>>
学习运维决心书
查看>>
计算中英文混合字符串的宽度
查看>>
MASM32_SDKv10以及一些帮大家打包的东西
查看>>
关于配置cordova的一些细节
查看>>
OpenJudge/Poj 2105 IP Address
查看>>
一分钟了解Allegro导入DXF文件
查看>>
Jenkisn call Psexec
查看>>
山东理工ACM【1532】矩阵输出
查看>>
IE9访问EBSR12排版格局错位(uttons and Text Misalignment in FWK pop-ups)
查看>>
JavaScript对象的浅拷贝与深拷贝
查看>>
SpringMVC学习笔记:表单提交 参数的接收
查看>>
idea全局设置
查看>>
Digital Color Meter 颜色值提取工具
查看>>
动态规划——Remove Boxes
查看>>
测试用例设计--因果图
查看>>
sublime sublimecodeintel配置 python
查看>>