如何创建DLL,以及注入DLL

为了防止忘记,特记下

DLL的创建,在VS2017中选择dll的创建

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

#include <Windows.h>
#include <stdio.h>

HMODULE thisModule;
HHOOK hook;
LRESULT CALLBACK LaunchListener(int nCode, WPARAM wParam, LPARAM lParam);
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

#ifdef __cplusplus    //If used by C++ code.
extern "C" {        //we need to export the C interface
#endif
    _declspec(dllexport) HHOOK AttachHook(DWORD threadID) {
        hook = SetWindowsHookEx(WH_CALLWNDPROC, LaunchListener, thisModule, threadID); //WH_KEYBOARD_LL  WH_CALLWNDPROC

        return hook;
    }
#ifdef __cplusplus
}
#endif
LRESULT CALLBACK LaunchListener(int nCode, WPARAM wParam, LPARAM lParam) {
    // process event here
    if (nCode >= 0) {
        CWPSTRUCT* cwp = (CWPSTRUCT*)lParam;
        if (cwp->message == WM_ACTIVATE) {
            if (LOWORD(cwp->wParam) == WA_INACTIVE)
            {
                //the window being deactivated
                MessageBox(NULL, TEXT("deactivated"), NULL, MB_OK);
            }
            else
            {
                //the window being activated
                MessageBox(NULL, TEXT("activated"), NULL, MB_OK);
            }
        }
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

Build上面的程序,会生成dll

在需要dll的另一个项目中注入dll

#include <Windows.h>
#include <stdio.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    if (message == WM_DESTROY) {
        PostQuitMessage(0);
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
};
HINSTANCE hinst;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
    HWND hwnd;

    hinst = GetModuleHandle(NULL);
    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = "hooking";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    hwnd = CreateWindow("hooking", "hooking", WS_OVERLAPPEDWINDOW | WS_EX_APPWINDOW | WS_EX_WINDOWEDGE , 0, 0, 500, 400, NULL, NULL, hinst, NULL);

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);

    DWORD threadID = GetWindowThreadProcessId(hwnd, NULL);

    HINSTANCE hinstDLL = LoadLibrary(TEXT("D:\\Strive Sun Project\\HOOK_1024\\DLL\\Debug\\DLL.dll")); //这里需要新创建的dll的路径
    HHOOK(*AttachHookProc)(DWORD);
    AttachHookProc = (HHOOK(*)(DWORD)) GetProcAddress(hinstDLL, "AttachHook");

    HHOOK HOOK = AttachHookProc(threadID);

    MSG msg = {};

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

这个项目是检测创建的窗口是否处于激活状态,仅供参考

 

另注: 如果想调试dll,需要在另一个程序加载DLL文件之前(可以设置Messagebox阻塞程序执行到下一步)将dll程序attach到另一个程序的.exe可执行文件上,再运行.exe即可调试dll

posted @ 2019-10-25 14:43  strive-sun  阅读(1005)  评论(0编辑  收藏  举报