HOOK还是不明白,请高手指点

prglib 2002-02-05 11:25:41
// KBHookLib.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "KBHookLib.h"

// This is an example of an exported variable
#pragma data_seg("Shared")
KBHOOKLIB_API HWND g_hWnd =0;
KBHOOKLIB_API HHOOK g_hHook =0;
KBHOOKLIB_API HINSTANCE g_hModDll =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:
if(g_hHook)
EjectCode();
break;
}
return TRUE;
}

//Hook procedure will send message to KBSpy
static LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam )
{
PMSG pMsg = PMSG(lParam);
if(pMsg->message == WM_CHAR)
::PostMessage(g_hWnd,WM_KEYRECIEVED,pMsg->wParam,pMsg->lParam);
return CallNextHookEx(g_hHook,nCode,wParam,lParam);
}

KBHOOKLIB_API void EjectCode()
{
::UnhookWindowsHookEx(g_hHook);
g_hHook = 0;
}


KBHOOKLIB_API BOOL InjectCode(HWND hWnd)
{
g_hWnd = hWnd;
//Hook up all processes if dwThreadID is assigned to zero
if(g_hHook = ::SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,g_hModDll, 0))
return TRUE;
else
return FALSE;
}


// KBSpyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "KBSpy.h"
#include "KBSpyDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma comment(lib,"e:\\myapp\\kbhooklib\\debug\\kbhooklib.lib")
/////////////////////////////////////////////////////////////////////////////
// CKBSpyDlg dialog

CKBSpyDlg::CKBSpyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CKBSpyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CKBSpyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKBSpyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CKBSpyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKBSpyDlg, CDialog)
//{{AFX_MSG_MAP(CKBSpyDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_KEYRECIEVED,KeyboardMsg)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKBSpyDlg message handlers

BOOL CKBSpyDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CKBSpyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
//Hide the window that will spy all keyboard input
// ShowWindow(SW_HIDE);
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CKBSpyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

LRESULT CKBSpyDlg::KeyboardMsg(WPARAM wParam, LPARAM lParam)
{
CString temp;
temp.Format("The key pressed is :%c",wParam);
::MessageBox(NULL,LPCTSTR(temp),NULL,MB_OK);
return 0;
}


int CKBSpyDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;

return InjectCode(GetSafeHwnd());
}
我把hhook改成共享的数据了,并且将它也导出了.但还是只能挂起自己的线程,我在notepad里打字
win98根本就不callback我的GetMsgProc
还请高手指点一二.
...全文
63 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
NowCan 2002-02-05
  • 打赏
  • 举报
回复
换成CALLWNDPROC钩子。

1,660

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 非技术类
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧