简体   繁体   English

为什么 openprocess function 每次都返回不同的句柄?

[英]why openprocess function return different handle each time?

I want to get the process and thread handles about some games to inject dll, and I used OpenProcess() and OpenThread() to obtain these handles.我想获取一些游戏的进程和线程句柄来注入dll,我使用了OpenProcess()OpenThread()来获取这些句柄。 But I found that I just get different handles each time I use these functions.但我发现每次使用这些函数时,我都会得到不同的句柄。 And they are useless for me because they arent the true handles.它们对我来说毫无用处,因为它们不是真正的手柄。 Please tell me how I can get the true handles?请告诉我如何才能获得真正的手柄?

Thanks for your answers and comments.感谢您的回答和评论。 And I found that I did not describe my problem very well.我发现我没有很好地描述我的问题。 Sorry.对不起。

Actually, if i used CreateProcess() funtion to launch a process and get handles from parameter lpProcessInformation pi .实际上,如果我使用CreateProcess()函数启动进程并从参数lpProcessInformation pi获取句柄。 I could inject my dll into game through these handles named pi.hProcess and pi.hThread .我可以通过这些名为pi.hProcesspi.hThread的句柄将我的 dll 注入游戏。 And these handles seem like would not change during the program's runtime.这些句柄似乎在程序运行时不会改变。

But if I got handles from OpenProcess() and OpenThread() , the process handle and thread handle were not same as the handle from CreateProcess() even though I got them in same run from a process.但是,如果我从OpenProcess()OpenThread()中获得句柄,则进程句柄和线程句柄与CreateProcess()中的句柄不同,即使我在同一进程中获得了它们。

So I thought that the handle from pi is the true handle, and the handle from OpenProcess() are fake.所以我认为来自pi的句柄是真正的句柄,而来自OpenProcess()的句柄是假的。 I dont know why they are different and why only handles from pi can work well.我不知道它们为什么不同,为什么只有pi的句柄才能正常工作。

Please tell me the difference about handles from OpenProcess() and CreateProcess() .请告诉我OpenProcess()CreateProcess()关于句柄的区别。 Or how I can get the handles same as CreateProcess() through PID.或者我如何通过 PID 获得与CreateProcess()相同的句柄。

This is the code how inject dll.这是如何注入 dll 的代码。 And ony handles from pi.hProcess and pi.hThread can work.并且只有来自pi.hProcesspi.hThread的句柄可以工作。

void KInject::InjectDll(HANDLE hProcess, HANDLE hThread, ULONG_PTR param){  
    QueueUserAPC(
        (PAPCFUNC)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA"),
        hThread, 
        (ULONG_PTR)param
        );  
}

void KInject::Inject2(HANDLE hProcess, HANDLE hThread, const char* szDLL ){
    SIZE_T len = strlen(szDLL) + 1;
    PVOID param = VirtualAllocEx(hProcess, NULL, len, MEM_COMMIT | MEM_TOP_DOWN /*MEM_RESERVE*/, PAGE_READWRITE);  
    if (param != NULL)  
    {  
        SIZE_T ret;
        if (WriteProcessMemory(hProcess, param, szDLL, len, &ret)) {  
            InjectDll(hProcess, hThread, (ULONG_PTR)param );   
        }  
    }
}

This is the code how i get handles.这是我如何获得句柄的代码。

#include <Windows.h>
#include "tlhelp32.h"
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    HWND hq = FindWindow(NULL, "Temp");
    RECT rect;
    DWORD dwThreadID;
    DWORD dwProcessId;
    GetWindowThreadProcessId(hq, &dwProcessId);
    GetWindowRect(hq, &rect);
    DWORD a = GetWindowThreadProcessId(hq, &dwProcessId);

    THREADENTRY32 te32 = { sizeof(te32) };
    HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (Thread32First(hThreadSnap, &te32))
    {
        do {
            if (dwProcessId == te32.th32OwnerProcessID)
            {
                dwThreadID = te32.th32ThreadID;
                break;
            }
        } while (Thread32Next(hThreadSnap, &te32));
    }
    CloseHandle(hThreadSnap);
    HANDLE  hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);  
    HANDLE  hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dwThreadID);
    CloseHandle(hThread);
    CloseHandle(hProcess);
    return 0;
}

There is nothing wrong with the API in this regard. API在这方面没有任何问题。 Their return values are just what they are supposed to be, ie "handles" to the actual processes and threads.它们的返回值正是它们应有的值,即实际进程和线程的“句柄”。 Exactly the same way as when you open a file, you get a handle to it, and if you open the same file multiple times, you may get different handles.与打开文件的方式完全相同,您会获得它的句柄,如果您多次打开同一个文件,您可能会获得不同的句柄。

Having said that, just in the same way that files do have a more permanent name—which is their paths —processes and threads also do have a more permanent name and its called their "ID".话虽如此,就像文件确实有一个更永久的名称(即它们的路径)一样,进程和线程也确实有一个更永久的名称,并将其称为“ID”。

You can use the Win32 functions GetProcessId(HANDLE handle) and GetThreadId(HANDLE handle) to get to these more permanent identifiers.您可以使用 Win32 函数GetProcessId(HANDLE handle)GetThreadId(HANDLE handle)来获取这些更永久的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM