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. Heartbleed漏洞利用程序

    #!/usr/bin/python # Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford (jspenguin@jspe ...

  2. 利用JS实现在li中添加或删除class属性

    $( function() { $("#test li").click(function(){ $("#test li").removeClass(" ...

  3. Excel 对应.xml/.ftl 配置(中爆导出范文)

    <?xml version="1.0"?><Workbook xmlns="urn:schemas-microsoft-com:office:sprea ...

  4. How to backup a remote PostgreSQL db and restore it locally?

    pg_dump and pg_restore 来备份和恢复数据库中的数据. 原文:  https://ksearch.wordpress.com/2012/09/28/how-to-backup-a- ...

  5. PuTTY连接Linuxserver常常断线解决方式

    PuTTY在远程连接server之后.常常会断线提示"Software caused connection abort",并且常常在非常短的时间内就失去连接. 解决方式例如以下: ...

  6. Struts2数据类型转换之批量数据转换

    前面我们实现了从字符串到User对象的转换.如果表单中有多个User数据,我们可以批量转换. 我们把input.jsp修改为: <h1>使用分号隔开username password< ...

  7. Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)

    http://www.jb51.net/article/38473.htm 首先启动命令行 1.在命令行运行:taskkill /f /im mysqld-nt.exe 下面的操作是操作mysql中b ...

  8. 微信小程序flex容器属性详解

    flex容器属性详解 flex-direction决定元素的排列方向 flex-wrap决定元素如何换行 flex-flow 是 flex-direction 和flex-wrap的简写 justif ...

  9. hibernate.cfg.xml文件连接mySql、Oracle、SqlServer配置

    1.连接mySql,文件配置如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibe ...

  10. Python基础之字符串的练习

    练习1 #!/usr/bin/python -tt # Copyright 2010 Google Inc. # Licensed under the Apache License, Version ...