感谢 Ben Volgt 在上面提供的帮助!
编辑:注意,正如 Ben Volgt 所说,您必须确定您可以及时拦截该过程,以便在加载 DLL 之前更改目录。因此,这并不总是有效,尽管在我的情况下它确实有效。
如果有人想拦截加载位置的进程,可以在这里找到代码:
void AddDirectory(HANDLE Handle, const char* DllPath)
{
if (!Handle)
{
//Error Message or Redirect
}
LPVOID AddDllDirAddr = (LPVOID)GetProcAddress(GetModuleHandleA("kernel32.dll"), "SetDllDirectoryA");
if (!AddDllDirAddr)
{
//Error Message or Redirect
}
LPVOID Alloc = VirtualAllocEx(Handle, NULL, strlen(DllPath), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!Alloc)
{
//Error Message or Redirect
}
WriteProcessMemory(Handle, Alloc, DllPath, strlen(DllPath), NULL);
HANDLE Thread = CreateRemoteThread(Handle, NULL, NULL, (LPTHREAD_START_ROUTINE)AddDllDirAddr, Alloc, 0, NULL);
if (!Thread)
{
//Error Message or Redirect
}
WaitForSingleObject(Thread, INFINITE);
VirtualFreeEx(Handle, Alloc, strlen(DllPath), MEM_RELEASE);
CloseHandle(Thread);
CloseHandle(Handle);
}