void Code::asm_code_inject(HANDLE handle)
{
LPVOID addr = VirtualAllocEx(handle, nullptr, this->length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (addr == nullptr)
return;
DWORD write_size = 0;
BOOL ret = WriteProcessMemory(handle, addr, this->code, this->length, &write_size);
if (ret == 0 || write_size != this->length)
{
VirtualFreeEx(handle, addr, 0, MEM_RELEASE);
return;
}
HANDLE thread = CreateRemoteThread(handle, nullptr, 0, LPTHREAD_START_ROUTINE(addr), nullptr, 0, nullptr);
if (thread == nullptr)
{
VirtualFreeEx(handle, addr, 0, MEM_RELEASE);
return;
}
[[maybe_unused]] DWORD wait_status = WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
VirtualFreeEx(handle, addr, 0, MEM_RELEASE);
#ifdef _DEBUG
std::wcout << L"Wait Status: " << wait_status << std::endl;
std::wcout << L"Asm Code: ";
for (size_t i = 0; i < this->length; i++)
std::cout << std::hex << int(code[i]) << " ";
std::cout << std::endl;
std::wcout << L"Code Length: " << std::dec << this->length << std::endl;
#endif
}
我要把这些从网上得到的代码(一部分)编译dll
[Error] cannot convert 'DWORD* {aka long unsigned int*}' to 'SIZE_T* {aka long long unsigned int*}' for argument '5' to 'WINBOOL WriteProcessMemory(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T*)'
[Warning] 'maybe_unused' attribute directive ignored [-Wattributes]
环境:ISOC++11