VC++ DLL的创建与动态调用模板

这个写了很多次了

 

C++

 

typedef BOOL (__stdcall *INSTALLHOOK)();
typedef BOOL (__stdcall *UNINSTALLHOOK)();

INSTALLHOOK InstallHook = NULL;
UNINSTALLHOOK UninstallHook = NULL;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     HMODULE hModule  = LoadLibrary("D://MyProjects//PASS_GetWindowText//Main//Dll//Debug//Dll.dll");
    if (hModule != NULL)
    {
        trace("LoadLibrary ok/n");
        InstallHook = (INSTALLHOOK)GetProcAddress(hModule, "InstallHook");
        UninstallHook = (UNINSTALLHOOK)GetProcAddress(hModule,"UninstallHook");
    }
    InstallHook();

    UninstallHook();

    FreeLibrary(hModule);


    return 0;
}

 

 

DLL文件CPP

 

#include "stdafx.h"
#include <stdio.h>

#pragma data_seg("Shared")
HHOOK g_hHook = NULL;
HMODULE g_hModDll = NULL;
bool bCreated = false;
DWORD g_OldWndProc = 0;
#pragma data_seg()

#pragma comment(linker, "/Section:Shared,rws")

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
        case   DLL_PROCESS_ATTACH:     
            g_hModDll = (HINSTANCE)hModule;     
            break;     
        case   DLL_THREAD_ATTACH:     
        case   DLL_THREAD_DETACH:     
        case   DLL_PROCESS_DETACH:
            break;
    };
   
    return TRUE;
}

LRESULT   CALLBACK   CallWndRetProc(int   nCode,   WPARAM   wParam,   LPARAM   lParam   )     
{
    switch (pmsg->message)
    {
    case WM_GETTEXT:
        break;
    case WM_KILLFOCUS:
        break;
    case WM_SETFOCUS:
        break;
    }
    return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}


BOOL __stdcall InstallHook()
{
    if(g_hHook   =   ::SetWindowsHookEx(WH_CALLWNDPROCRET,CallWndRetProc,g_hModDll,   0))     
        return   TRUE;     
    else     
        return   FALSE;   
}

BOOL __stdcall UninstallHook()
{
    if (g_hHook != NULL)
    {
        ::UnhookWindowsHookEx(g_hHook);     
        g_hHook   =   NULL;   
    }
    return TRUE;
}

 

DLL文件DEF

 

EXPORTS InstallHook
EXPORTS UninstallHook

你可能感兴趣的:(vc++)