消息钩子入门篇(4)---示例__外壳钩子(WH_SHELL)


这个例子程序完成记录系统所有启动的程序的功能,并保存到文件中。
依然建立的是MFC扩展动态链接库。步骤同前面的例子。
ShellHook.h文件中声明导出类
class AFX_EXT_CLASS CShellHook:public CObject
{
public:
    CShellHook();
    ~CShellHook();
    void SetShellHook();
    void unSetShellHook();
};

ShellHook.cpp文件的内容:

#include "stdafx.h"
#include <afxdllx.h>
#include "ShellHook.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HHOOK glShellHook;
HINSTANCE hInst;
LRESULT CALLBACK ShellHookProc(int nCode,WPARAM wParam,LPARAM lParam);
void SaveLog(CString str);
static AFX_EXTENSION_MODULE ShellHookDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    // Remove this if you use lpReserved
    UNREFERENCED_PARAMETER(lpReserved);

    if (dwReason == DLL_PROCESS_ATTACH)
    {
        TRACE0("SHELLHOOK.DLL Initializing!/n");
       
        // Extension DLL one-time initialization
        if (!AfxInitExtensionModule(ShellHookDLL, hInstance))
            return 0;

        // Insert this DLL into the resource chain
        // NOTE: If this Extension DLL is being implicitly linked to by
        //  an MFC Regular DLL (such as an ActiveX Control)
        //  instead of an MFC application, then you will want to
        //  remove this line from DllMain and put it in a separate
        //  function exported from this Extension DLL.  The Regular DLL
        //  that uses this Extension DLL should then explicitly call that
        //  function to initialize this Extension DLL.  Otherwise,
        //  the CDynLinkLibrary object will not be attached to the
        //  Regular DLL's resource chain, and serious problems will
        //  result.

        new CDynLinkLibrary(ShellHookDLL);
        hInst=hInstance;
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
        TRACE0("SHELLHOOK.DLL Terminating!/n");
        // Terminate the library before destructors are called
        AfxTermExtensionModule(ShellHookDLL);
    }
    return 1;   // ok
}
CShellHook::CShellHook()
{
}
CShellHook::~CShellHook()
{
unSetShellHook();
}
void CShellHook::SetShellHook()
{
    glShellHook=(HHOOK)SetWindowsHookEx(WH_SHELL,ShellHookProc,hInst,0);
}
void CShellHook::unSetShellHook()
{
    if (glShellHook)
    {
        BOOL bRet=UnhookWindowsHookEx(glShellHook);
        if (bRet)
        {
            glShellHook=NULL;
        }
    }
}
LRESULT CALLBACK ShellHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
    if (nCode==HSHELL_WINDOWCREATED)
    {
        CWnd* pwnd=CWnd::FromHandle((HWND)wParam);
        CString strName;
        pwnd->GetWindowText(strName);
        SaveLog(strName);
        if (strName=="扫雷")
        {
           PostMessage(pwnd->GetSafeHwnd(),WM_CLOSE,0,0);
        }
    }
    return 0;
}
void SaveLog(CString str)
{
    CTime tm=CTime::GetCurrentTime();
    CString name;
    name.Format("D://shellhook_%d_%d.log",tm.GetMonth(),tm.GetDay());
    CFile file;
    if (!file.Open(name,CFile::modeReadWrite))
    {
        file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
    }
    file.SeekToEnd();
    file.Write(str.GetBuffer(128),str.GetLength());
    CString strTime;
    strTime.Format("/t%02d:%02d:%02d/r/n",tm.GetHour(),tm.GetMinute(),tm.GetSecond());
    file.Write(strTime.GetBuffer(128),strTime.GetLength());
    file.Close();
}
学习内容:文件操作中的问题,(1)文件定位到文件末尾(2)在文本文件中输入回车符用"/r/n"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值