我的問題很簡單,標題解釋了這一切。基本上,當我用Visual Studio 2013編譯我的程序時,dll注入完全正常。當我在Qt Creator中編譯完全相同的程序時,它不會。DLL注入工程,除非我在Qt Creator中編譯它
我似乎有同樣的問題,因爲這傢伙:Why does Qt not work with dll injection?
這裏是我的代碼:
Injector.h
#ifndef INJECTOR_H_INCLUDED
#define INJECTOR_H_INCLUDED
#include <Windows.h>
#include <string>
class Injector
{
public:
/**
* Loads a DLL into the remote process
* @Return true on sucess, false on failure
*/
bool InjectDll(DWORD processId, std::string dllPath);
private:
};
#endif // INJECTOR_H_INCLUDED
Injector.cpp
#include "Injector.h"
bool Injector::InjectDll(DWORD processId, std::string dllPath)
{
HANDLE hThread, hProcess;
void* pLibRemote = 0; // the address (in the remote process) where szLibPath will be copied to;
HMODULE hKernel32 = GetModuleHandleA("Kernel32");
char DllFullPathName[_MAX_PATH];
GetFullPathNameA(dllPath.c_str(), _MAX_PATH, DllFullPathName, NULL);
printf("Loading dll: %s\n", DllFullPathName);
// Get process handle
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
// copy file path in szLibPath
char szLibPath[_MAX_PATH];
strcpy_s(szLibPath, DllFullPathName);
// 1. Allocate memory in the remote process for szLibPath
pLibRemote = VirtualAllocEx(hProcess, NULL, sizeof(szLibPath), MEM_COMMIT, PAGE_READWRITE);
if (pLibRemote == NULL)
{
printf("Couldn't allocate memory, please restart with administrator privileges\n");
return false;
}
// 2. Write szLibPath to the allocated memory
WriteProcessMemory(hProcess, pLibRemote, (void*)szLibPath, sizeof(szLibPath), NULL);
// 3. Force remote process to load dll
hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryA"), pLibRemote, 0, NULL);
if (hThread == NULL)
{
printf("Couldn't load DLL");
return false;
}
printf("Dll successfully loaded\n");
return true;
}
的main.cpp
#include "injector.h"
int main(int argc, char *argv[])
{
Injector inject;
DWORD processId = 6224;
inject.InjectDll(processId, "MyDLL.dll");
system("pause");
}
這是DLL(我用在這兩個方案中的同一個DLL,我沒有重新編譯):
#include <Windows.h>
#include <stdio.h>
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
AllocConsole();
freopen("CONOUT$", "w", stdout);
printf("base address: %X\n", (DWORD)GetModuleHandle(NULL));
break;
case DLL_PROCESS_DETACH:
FreeConsole();
}
return TRUE;
}
在VS2013編譯正確的程序注入DLL,而編譯在Qt Creator中的程序說,DLL注入成功,但DLL永遠不會被注入。
注:我試圖注入的程序在這兩種情況下都是相同的,並且不是用Qt製作的。
以下是編譯器輸出:
的Visual Studio:
CL/C /紫/ W3/WX-/ SDL/O2 /愛/ Oy-/GL/d _CRT_SECURE_NO_WARNINGS/D/ _MBCS/Gm-/EHsc/MD/GS/Gy/fp: errorReport:prompt Injector.cpp main.cpp
的Qt:
C:\ Qt的\ Qt5.4.0 \工具\ QtCreator \ BIN \ jom.exe -f Makefile.Release CL -c -nologo -Zm200 -Zc:wchar_t的-FS -02 -MD -Zc:strictStrings -GR -W3 -w34100 -w34189 -EHsc -DUNICODE -DWIN32 -DWIN64 -DQT_NO_DEBUG -DQT_CORE_LIB -DNDEBUG -I「C:\ Qt \ Qt5.4.0 \ 5.4 \ msvc2013_64_opengl \ include」-I「C :\ Qt \ Qt5.4.0 \ 5.4 \ msvc2013_64_opengl \ include \ QtCore「-I」釋放「-I」。「 -I「C:\ Qt \ Qt5.4.0 \ 5.4 \ msvc2013_64_opengl \ mkspecs \ win32-msvc2013」-Forelease \ @C:\ Users \ JFG \ AppData \ Local \ Temp \ injector.obj.7040.0.jom injector.cpp link/NOLOGO/DYNAMICBASE/NXCOMPAT/INCREMENTAL:NO /SUBSYSTEM:CONSOLE「/ MANIFESTDEPENDENCY:type ='win32' name ='Microsoft.Windows.Common-Controls'version ='6.0.0.0' publicKeyToken ='6595b64144ccf1df'language = '' ProcessorArchitecture用於=''」 /清單:嵌入 /OUT:release\test_dll_inection_qt.exe @C:\用戶\ JFG \應用程序數據\本地\ TEMP \ test_dll_inection_qt.exe.7040.469.jom
任何幫助將不勝感激,謝謝。
的VS和Qt設置了什麼樣的編譯器標誌? – txtechhelp 2015-01-20 21:30:29
我不完全知道如何查看哪些標誌Qt的設置,但我並沒有通過自己在任何VS或Qt的 – MyUsername112358 2015-01-20 21:33:26
添加任何VS有一定的默認編譯器選項設置,如果什麼都不做(即空白項目,並開始編譯) ,在VS和Qt中,你可以在'編譯器輸出'窗口中看到編譯器正在被輸入的內容(正在使用的選項)(可能需要在IDE中切換一些設置) – txtechhelp 2015-01-20 21:42:10