gpt4 book ai didi

c++ - LoadLibraryW 在 LoadLibraryA 完成工作时不起作用

转载 作者:行者123 更新时间:2023-11-28 01:12:42 28 4
gpt4 key购买 nike

我编写了一些示例程序和 DLL 来学习 DLL 注入(inject)的概念。

我将DLL注入(inject)示例程序的注入(inject)代码如下(错误处理省略):

std::wstring dll(L"D:\\Path\\to\\my\\DLL.dll");
LPTHREAD_START_ROUTINE pLoadLibraryW =
(LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryW");
int bytesNeeded = WideCharToMultiByte(CP_UTF8, 0, dll.c_str(), dll.length(),
NULL, 0, NULL, NULL);
std::vector<byte> dllName(bytesNeeded);
WideCharToMultiByte(CP_UTF8, 0, dll.c_str(), dll.length(),
(LPSTR)&dllName[0], bytesNeeded, NULL, NULL);
// Memory is a class written by me to simplify memory processes.
// Constructor takes desired permissions.
Memory mem (pid, false, true, false, true, false, false, false,
false, false, true, true, true, false);
// Ensures deletion of the allocated range.
// true / true / false = read and write access, no execute permissions
std::tr1::shared_ptr<void> allocated =
mem.AllocateBytes(dllName.size(), true, true, false);
mem.WriteBytes((unsigned int)allocated.get(), dllName);
mem.CreateThread(pLoadLibraryW, allocated.get());

Memory::CreateThread如下:

void Memory::CreateThread(LPTHREAD_START_ROUTINE address, LPVOID parameter) const {
std::tr1::shared_ptr<void> hThread(CreateRemoteThread(m_hProcess.get(),
NULL, 0, address, parameter, 0, NULL), CloseHandle);
if (hThread.get() == NULL) {
throw std::runtime_error("Memory::CreateThread: CreateRemoteThread failed");
}
DWORD returned = WaitForSingleObject(hThread.get(), INFINITE);
if (returned != WAIT_OBJECT_0) {
throw std::runtime_error("Memory::CreateThread: The remote thread did not complete properly");
}
}

问题是模块没有加载。但是,当我将第二行更改为

LPTHREAD_START_ROUTINE pLoadLibraryW =
(LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryA");

它有效(因为测试 dll 的名称中没有 unicode 字符)。

如何让它与 LoadLibraryW 一起工作?

最佳答案

HMODULE WINAPI LoadLibrary(
__in LPCTSTR lpFileName
);

它需要一个 TCHAR——所以 LoadLibraryW 的参数必须是一个宽字符串;上面的代码传递了参数的多字节形式,这是 LoadLibraryA 想要的形式。

关于c++ - LoadLibraryW 在 LoadLibraryA 完成工作时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584502/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com