C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[38069] Re:Message Hook 사용법
유영인 Cuperido [cuperido] 1873 읽음    2004-11-17 05:42
Hook.zip 509.9KB Message Hook 예제
어디서 어떻게 안되시는지요?
예제를 원하시는것 같아 간단한 예제 만들어서 올려드립니다.

이전에 말씀하셨던, 키보드 감지해서 Msg.TXT 파일로 저장하는 형태입니다.
우선, DLL 파일의 사용에 관해서 참고하시고, SetWindowsHookEx에 관하여
레퍼런스를 참고하시는 것이 접근이 좀 더 쉬우실것 같습니다.


cuperido

실행 파일및 전체 프로젝트 파일은 첨부파일 참조하십시오.

** DLL 파일 **

//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#include <stdio.h>
#pragma hdrstop
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused

static bool STOP = false;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{

return 1;

}

extern "C" __declspec(dllexport) LRESULT CALLBACK GetMsgProc(INT itCode, WPARAM wpParam, LPARAM lpParam)
{

FILE              *fiHandle;
String            stData;
MSG               *msMessage;


msMessage = ((MSG*)lpParam);

if(msMessage->message) STOP = false;
                   else STOP = true;

if(msMessage->message == WM_KEYDOWN || msMessage->message == WM_KEYUP || msMessage->message == WM_CHAR) {
   char            caCaption[256];

   GetWindowText(msMessage->hwnd, caCaption, 256);

     fiHandle = fopen("Msg.txt", "a+");

     if(fiHandle) {
       stData = Format("Handle : %0.8x(%s), Message : %d, LParam : %d, WParam : %d\n",
                ARRAYOFCONST(((int)msMessage->hwnd, (String)caCaption, (int)msMessage->message, (int)msMessage->wParam, (int)msMessage->lParam)));

       fputs(stData.c_str(), fiHandle);
     }

     fclose(fiHandle);
}

return (STOP) ? TRUE : CallNextHookEx(NULL, itCode, wpParam, lpParam);

}


** CPP 파일 **

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit_Hook.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"

TMain              *Main;
HINSTANCE          HOOK;
HOOKPROC           HOOKFUNC;
HHOOK              HOOKHANDLE;

//---------------------------------------------------------------------------

__fastcall TMain::TMain(TComponent* Owner) : TForm(Owner)
{
}

//---------------------------------------------------------------------------

void __fastcall TMain::FormCreate(TObject *Sender)
{

DeleteFile("Msg.txt");

HOOK = LoadLibrary("HookDll.DLL");

if(!HOOK) {
   ShowMessage("로드 실패");
   return;
}

HOOKFUNC   = (HOOKPROC)GetProcAddress(HOOK, "GetMsgProc");
HOOKHANDLE = SetWindowsHookEx(WH_GETMESSAGE, HOOKFUNC, HOOK, 0);

if(!HOOKHANDLE) {
   ShowMessage("Hook 실패");
}

}
//---------------------------------------------------------------------------
void __fastcall TMain::FormClose(TObject *Sender, TCloseAction &Action)
{

UnhookWindowsHookEx(HOOKHANDLE);
FreeLibrary(HOOK);

}
//---------------------------------------------------------------------------



이성제 님이 쓰신 글 :
: 흠.. 하루 종일 연구 한 결과
:
: 알아낸것은.. 글쎄 안된다는것입니다. ㅎ
:
: 사용법을 알수가 없으니 응용또한 불가능 하죠 ㅎ
:
: 사용법정도 가르쳐 주십시요 ㅎ

+ -

관련 글 리스트
38065 Message Hook 사용법 이성제 969 2004/11/17
38069     Re:Message Hook 사용법 유영인 Cuperido 1873 2004/11/17
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.