当某个进程不活动时如何防止我的 dll 运行

How to prevent my dll from running when a certain process isn't active

所以我想让我的 dll 只能由我的注入器注入,我认为一个好方法是只让我的 dll 在注入器为 运行 时打开。但是我不知道该怎么做。

正如 Mayur 在评论中提到的,您的 DLL 可以 enumerate running processes using EnumProcesses(), and get their filenames looking for a match to your injector. You can do that in your DLL's entry point functionDLL_PROCESS_ATTACH 阶段。如果未检测到注入器,您的入口点可以 return FALSE 来中止加载。

或者,检测注入器的一种更简单的方法是让它通过 CreateMutex() in the global kernel namespace, and then have the DLL entry point try to access that same object via OpenMutex(), and fail to load if the mutex does not exist. See Using Named Objects 创建一个命名的互斥锁以获取更多详细信息。