DLL注入工作,除非我在Qt Creator中编译它

时间:2015-01-20 21:20:40

标签: c++ qt dll

我的问题很简单,标题解释了这一切。基本上,当我使用Visual Studio 2013编译我的程序时,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 / Zi / W3 / WX- / sdl / O2 / Oi / Oy- / GL / D _CRT_SECURE_NO_WARNINGS / D   _MBCS / Gm- / EHsc / MD / GS / Gy / fp:exact / Zc:wchar_t / Zc:forScope / Fo“Release \”/ Fd"Release\vc120.pdb“/ Gd / TP / analyze-   / errorReport:提示Injector.cpp main.cpp

Qt的:

  

C:\ Qt \ Qt5.4.0 \ Tools \ QtCreator \ bin \ jom.exe -f Makefile.Release cl -c   -nologo -Zm200 -Zc:wchar_t -FS -O2 -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:没有   / SUBSYSTEM:CONSOLE“/ MANIFESTDEPENDENCY:type ='win32'   name ='Microsoft.Windows.Common-Controls'version ='6.0.0.0'   publicKeyToken ='6595b64144ccf1df'language =''   processorArchitecture =''“/ MANIFEST:embed   /OUT:release\test_dll_inection_qt.exe   @C:\用户\ JFG \应用程序数据\本地\ TEMP \ test_dll_inection_qt.exe.7040.469.jom

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

问题是Qt用64位编译我的程序,而visual studio用32位编译它。

我仍然不确定为什么在32位目标中注入32位dll因为64位喷油器而失败,但是现在我解决了我的问题......