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

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 = ;
    HANDLE hThread = NULL;
    DWORD dwThreadId = ;
    PMYDATA pData;

hProcess = GetCurrentProcess(); //进程伪句柄
    cout << "The Current Process Pseudo Handle is " << hProcess << endl;
    DuplicateHandle(GetCurrentProcess(),
        GetCurrentProcess(),
        GetCurrentProcess(),
        &hProcess,
        ,
        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();
    }
    // Generate unique data for each thread.
    pData->nVal1 = ;
    pData->nVal2 = ;
    // Create Thread
    hThread = CreateThread( NULL,               // default security attributes
                            ,                  // use default stack size  
                            MyThread,           // thread function 
                            pData,              // argument to thread function 
                            ,                  // use default creation flags 
                            &dwThreadId);       // returns the thread identifier

cin.get();
    return ;
}

DWORD WINAPI MyThread( LPVOID lpParam )
{
    HANDLE hThread = NULL;
    DWORD dwThreadId = ;
    hThread = GetCurrentThread();   //线程伪句柄
    cout << "The Current Thread Pseudo Handle is " << hThread << endl;
    DuplicateHandle(GetCurrentProcess(),
        GetCurrentThread(),
        GetCurrentProcess(),
        &hThread,
        ,
        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] = {};
    size_t cchStringSize;
    DWORD dwChars;

hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if( hStdout == INVALID_HANDLE_VALUE )
        return ;

// 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 ;

}

Win API:之GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId的更多相关文章

  1. C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie

    网上找到的可以完整获取Cookie的方法,转载一下希望能帮助更多人. 亲测可用 在Winform中使用WebBrowser控件获取网站的Cookie有时候是不完整的,默认调用Document.Cook ...

  2. 文件操作(CRT、C++、WIN API、MFC)

    一.使用CRT函数文件操作 二.使用标准C++库 std::fstream std::string 1)std::string对象内部存储了一个C的字符串,以'\0'结尾的. 2)std::strin ...

  3. 黑客编程教程(二)Win API编程简介

    第二节 Win API编程简介 下面介绍一下WIN API. 我们需要自己编写一个工具时,必然会用到很多操作windows和控制windows的函数,这些函数就是windows API. API是Ap ...

  4. C# 调用win api获取chrome浏览器中地址

    //FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...

  5. 试来试去,WIN下最简单的WIN API开发工具,Pelles C就好啦

    昨晚试过N个,不是太大,就是不容易和WIN API集成. 今早一试就灵了个.... Pelles C. Pelles C是一款windows下的C IDE,支持调试,且为免费.它有一个高效率的链接器, ...

  6. [原创] Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍

    Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍 函数原型:HWND HtmlHelpA( HWND hwndCaller, LPCSTR pszFile, UINT uCo ...

  7. Delphi Win API 函数 MulDiv

    Delphi Win API 函数 MulDiv 原型:function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer; st ...

  8. Win API 内存整理

    记得我的笔记本上曾经安装了一款名为内存整理大师的软件,当时觉得挺好用而且挺NB的,就是导致开机启动有点慢. 当时我就在想,内存整理是怎么实现的?不过那是水平实在是不怎么样,估计连windows程序的消 ...

  9. MFC--串口编程---WIN API的方式将串扣操作封装在线程类中

    串口采集数据 本文档介绍的是如何获取串口原始数据并将原始数据解析成可处理或可展示的数据. 一.串口采集有很多方式: 1).MFC有一个专门的控件,直接编程采集,一个控件只能采集一个串口,而且串口名字比 ...

随机推荐

  1. Linux Bash严重漏洞修复方法

    日前Linux官方内置Bash中新发现一个非常严重安全漏洞,黑客可以利用该Bash漏洞完全控制目标系统并发起攻击,为了避免Linux服务器受影响,就要尽快修补该漏洞了.(漏洞参考https://acc ...

  2. SpringMvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  3. Selenium——去掉Chrome正受到自动软件测试的控制

    selenium打开chrome浏览器时,地址栏下方会出现该提示“Chrome正在受到自动测试软件的控制” public class BasicDriver { public WebDriver dr ...

  4. SAP MM模块 经常使用函数

    1. MM_CURRENT_PRICE_DOCUMENT   2. ME_READ_HISTORY 获取採购行项目的历史记录   CALL FUNCTION 'ME_READ_HISTORY'     ...

  5. crm查询和删除审核历史记录

    using System;     using System.Linq;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query; ...

  6. Django的信号机制详解

    Django提供一种信号机制.其实就是观察者模式,又叫发布-订阅(Publish/Subscribe) .当发生一些动作的时候,发出信号,然后监听了这个信号的函数就会执行. Django内置了一些信号 ...

  7. Django——如何在Django模板中注入全局变量?——part1

    问题:TEMPLATE_CONTEXT_PROCESSORS代表着什么? 问题描述:无法在项目的settings.py文件中找到TEMPLATE_CONTEXT_PROCESSORS. ——————— ...

  8. olede读excel

    注意点:需要比较excel文件中是否有重复列时,需要设置HDR=No,IMEX=1,即把第一列当做数据读取,不然读到的datatable列名会被自动加数字后缀. /// < summary> ...

  9. android之ScrollView里嵌套ListView(转)

    hi,大家好,研究完ScrollView嵌套ScrollView之后,本人突然又想研究ScrollView里嵌套ListView了. 如果还不知道ScrollView嵌套ScrollView是怎么实现 ...

  10. 深入理解Tomcat虚拟文件夹

        我们知道,Web站点中的内容(包含网页,图片,音频文件等)一般都存放在App的文件夹下.但随着站点内容的不断丰富,用户须要把不同层次的内容组织成站点的子文件夹. 我们通常的做法是在站点主文件夹 ...