天天看點

ActiveX 鈎子屏蔽功能鍵

//以備後用

// test.cpp : Implementation of CTestApp and DLL registration.

#include "stdafx.h"

#include "test.h"

//

//附加頭

#include "winuser.h"

#define _WIN32_WINNT 0x0400

#define WH_KEYBOARD_LL 13

const byte LLKHF_ALTDOWN = 0x20;

/

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/

//附加函數

HHOOK installhook(void); //鈎子安裝函數

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);//挂鈎函數

BOOL uninstallhook(void); //拆卸鈎子

BOOL limitcontrol(void);

BOOL unlimitcontrol(void);

/

CTestApp NEAR theApp;

const GUID CDECL BASED_CODE _tlid =

        { 0x30534bf3, 0x7263, 0x434c, { 0x95, 0xa9, 0x3, 0xc1, 0x10, 0xbd, 0xd2, 0xf } };

const WORD _wVerMajor = 1;

const WORD _wVerMinor = 0;

//定義結構 變量

typedef struct {

    DWORD vkCode;

    DWORD scanCode;

    DWORD flags;

    DWORD time;

    ULONG_PTR dwExtraInfo;

} KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

static HHOOK phk=NULL;

// CTestApp::InitInstance - DLL initialization

BOOL CTestApp::InitInstance()

{

    BOOL bInit = COleControlModule::InitInstance();

    if (bInit)

    {

        // TODO: Add your own module initialization code here.

        ::MessageBox(NULL,"Active 加載","提示",MB_OK);

        limitcontrol();

        installhook();

    //    system("C:\\PROGRA~1\\INTERN~1\\iexplore -k www.baidu.com");

    }

    return bInit;

}

// CTestApp::ExitInstance - DLL termination

int CTestApp::ExitInstance()

{

    // TODO: Add your own module termination code here.

    uninstallhook();

    unlimitcontrol();

    ::MessageBox(NULL,"Active 退出","提示",MB_OK);

    return COleControlModule::ExitInstance();

}

/

// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)

{

    AFX_MANAGE_STATE(_afxModuleAddrThis);

    ::MessageBox(NULL,"Active 注冊","提示",MB_OK);

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))

        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))

        return ResultFromScode(SELFREG_E_CLASS);

    return NOERROR;

}

/

// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)

{

    ::MessageBox(NULL,"Active 解除安裝","提示",MB_OK);

    AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))

        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))

        return ResultFromScode(SELFREG_E_CLASS);

    return NOERROR;

}

HHOOK installhook()

{

    phk=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KeyboardProc,GetModuleHandle(NULL),0);

    if(phk==NULL)

        ::MessageBox(NULL,"鍵盤監控未啟動","提示",MB_OK);

        ::MessageBox(NULL,"鍵盤監控啟動","提示",MB_OK);

    return phk;

}

LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)

{

    if(HC_ACTION==nCode)

    {

          // ::MessageBox(NULL,"測試點","提示",MB_OK);

        switch(wParam)

        {

       case WM_KEYDOWN:    

       case WM_SYSKEYDOWN: 

       case WM_KEYUP:   

       case WM_SYSKEYUP: 

           PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;

            if((p->flags & LLKHF_ALTDOWN)!=0) return 1;  //屏蔽 tab + alt / alt

            switch(p->vkCode)

                {

                case VK_LWIN:   //左右 win功能鍵

                case VK_RWIN:

                case VK_F5:   

                case VK_F4:

                case VK_ESCAPE:    //屏蔽 esc

                case VK_F11:

                case VK_APPS:

                case VK_F1:

                      return 1;

                }

                break;

       }

     }

     return CallNextHookEx( phk, nCode, wParam, lParam );

}

BOOL uninstallhook()

{

     if(phk)

         if(!UnhookWindowsHookEx(phk))

               ::MessageBox(NULL,"鍵盤無法正常監控關閉","提示",MB_OK);

               ::MessageBox(NULL,"鍵盤監控關閉","提示",MB_OK);

     return true;

}

BOOL limitcontrol(void)

{

   HKEY hKey;

   DWORD wt = 1;

   if( (RegCreateKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",&hKey)==0 )&&( RegSetValueEx(hKey,"DisableTaskMgr",0,REG_DWORD,(const BYTE*)&wt,4)==0) )

       ::MessageBox(NULL,"禁用任務管理器","提示",MB_OK);

    else

       ::MessageBox(NULL,"禁用任務管理器失敗","提示",MB_OK);

    RegCloseKey(hKey);

    return true;

}

BOOL unlimitcontrol(void)

{

    HKEY hKey;

    if(RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",0,KEY_WRITE,&hKey)==0)

    {

        if(RegDeleteValue(hKey,"DisableTaskMgr")==0)

               ::MessageBox(NULL,"恢複任務管理器","提示",MB_OK);

        RegCloseKey(hKey);

         return true;

    }

          ::MessageBox(NULL,"任務管理器未有恢複","提示",MB_OK);

    return false;

}

繼續閱讀