thread_copy_win32

`#include <windows.h>
#include <iostream>

DWORD WINAPI YourThreadFunction(LPVOID lpParam)
{
HANDLE hSource = ::GetStdHandle(STD_OUTPUT_HANDLE); // 示例:原始句柄为标准输出句柄
HANDLE hTarget = NULL; // 用于存储复制后的句柄

if (::DuplicateHandle(::Ge…


This content originally appeared on DEV Community and was authored by 海前 王

`#include <windows.h>
#include <iostream>

DWORD WINAPI YourThreadFunction(LPVOID lpParam)
{
    HANDLE hSource = ::GetStdHandle(STD_OUTPUT_HANDLE); // 示例:原始句柄为标准输出句柄
    HANDLE hTarget = NULL; // 用于存储复制后的句柄

    if (::DuplicateHandle(::GetCurrentProcess(), hSource, ::GetCurrentProcess(), &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS))
    {
        // 成功复制句柄,使用 hTarget 作为复制后的句柄
        // 可以使用 hTarget 句柄进行操作

        // 示例:使用复制的句柄进行输出
        ::SetStdHandle(STD_OUTPUT_HANDLE, hTarget);
        std::cout << "This is printed using the duplicated handle." << std::endl;

        // 关闭复制的句柄
        ::CloseHandle(hTarget);
    }
    else
    {
        // 复制句柄失败
        DWORD lastError = GetLastError();
        // 处理错误
    }

    return 0;
}

int main()
{
    // 创建线程
    HANDLE hThread = ::CreateThread(NULL, 0, YourThreadFunction, NULL, 0, NULL);
    if (hThread == NULL)
    {
        // 创建线程失败
        DWORD lastError = GetLastError();
        // 处理错误
        return 1;
    }

    // 等待线程结束
    ::WaitForSingleObject(hThread, INFINITE);

    // 关闭线程句柄
    ::CloseHandle(hThread);

    // 示例:使用主线程的句柄进行输出
    std::cout << "This is printed using the original handle." << std::endl;

    return 0;
}`


This content originally appeared on DEV Community and was authored by 海前 王


Print Share Comment Cite Upload Translate Updates
APA

海前 王 | Sciencx (2024-08-27T01:16:31+00:00) thread_copy_win32. Retrieved from https://www.scien.cx/2024/08/27/thread_copy_win32/

MLA
" » thread_copy_win32." 海前 王 | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/thread_copy_win32/
HARVARD
海前 王 | Sciencx Tuesday August 27, 2024 » thread_copy_win32., viewed ,<https://www.scien.cx/2024/08/27/thread_copy_win32/>
VANCOUVER
海前 王 | Sciencx - » thread_copy_win32. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/thread_copy_win32/
CHICAGO
" » thread_copy_win32." 海前 王 | Sciencx - Accessed . https://www.scien.cx/2024/08/27/thread_copy_win32/
IEEE
" » thread_copy_win32." 海前 王 | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/thread_copy_win32/. [Accessed: ]
rf:citation
» thread_copy_win32 | 海前 王 | Sciencx | https://www.scien.cx/2024/08/27/thread_copy_win32/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.