In order to prevent forget that special note of
Create a DLL, choose to create the dll in VS2017
// 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 the above program will generate dll
In another project required dll injection dll's
#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); HinstDLL HINSTANCE = LoadLibrary (TEXT ( " D: \\ \\ HOOK_1024 Strive Sun Project Debug \\ \\ \\ DLL DLL.dll " )); // It should be the path to dll newly created 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); } }
This project was created to detect whether a window is active, for reference only
Another note: If you want to debug dll, DLL files required before another program is loaded (can be set Messagebox blocking program execution to the next step) will attach to the dll program executable .exe file of another program, and then run the .exe to debug dll