如何正确处理注入的DLL线程? [英] How to correctly dispose of injected DLL thread?

查看:345
本文介绍了如何正确处理注入的DLL线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个DLL注入到目标进程中,以便在播放MMORPG时充当助手(目前的功能将鼠标转换为鼠标点击,因为MMORPG需要用户将鼠标移动到特定的功能,我鄙视。 )

I'm injecting a DLL into a target process to act as a helper while playing an MMORPG (currently functionality converts key press into mouse clicks, as the MMORPG requires the user to move their mouse for certain functionality, something I despise.)

假设我想要注销我的DLL,无论什么原因,我该怎么办呢?这个方法是否干净?

Let's say I want to uninject my DLL for whatever reason, how would I go about it? Is this method clean?

bool running = true;
while (running) // This is the only thread I'm using, and it is running in "realtime"
{
    // Do keyboard handing stuff in switch statement
    case keys.EscapeKey: // If the escape key is pressed
        running = false; // Set the running bool to false, and break the loop
        break;
}

这是干净的吗?线程结束,所以我的dlluninject本身?

Is this clean? The thread ends, so does my dll "uninject" itself? Or does it still loiter and continue to consume the memory that I allocated when injecting?

感谢
Josh

Thanks Josh

推荐答案

我假设你使用的CreateRemoteThread的开始地址设置为LoadLibrary,并且你在注入DLL的DllMain中启动一个线程。

I assume that you used CreateRemoteThread with a start address set to LoadLibrary, and that you start a thread in the DllMain of the injected DLL.

首先,在DllMain DLL_PROCESS_ATTACH中保存一个全局变量的DLL的HMODULE。

First, in DllMain DLL_PROCESS_ATTACH save in a global variable the HMODULE of the DLL.

其次,将这个HMODULE传递给FreeLibraryAndExitThread,退出并卸载Dll。

Second, pass this HMODULE to FreeLibraryAndExitThread when you want your thread to exit and unload the Dll.

注意!你不能有生活代码留在你身后,也就是说,没有回传地址传递给任何API,如果回调在触发后卸载,将立即崩溃(或更糟)。

Beware! you must NOT have "living code" left behind you, that is, no callback address passed to whatever API, if the callback is trigered after the unload, that will be immediate crash (or worse).

这篇关于如何正确处理注入的DLL线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆