오후 2:28 2003-09-05
조경민  (bro@shinbiro.com)
전역후크시 잘안될때
=========================================================

typedef struct _tag0
{
        HINSTANCE        hmod;
        HWND                hwnd;
}HOOK_CONTEXT,*LPHOOK_CONTEXT;

// shared variable must be initialised.
#pragma data_seg(".shared")
        HOOK_CONTEXT        context = {0,}; // 구조체도 초기화해야함
        HINSTANCE        g_hmod = 0;
        HWND                g_hwnd = NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS") // RWS 소문자 안된다.

공유변수 초기화를 안하면 전역후크가 안되고 그냥 프로세스후크만됨 --;

-----------------------------------------------------------------------
관련 참조 URL
http://www.mooremvp.freeserve.co.uk/Win32/framed_tip004.htm

4. Why doesn't my system wide hook capture all processes ?
  

The usual reason for system-wide hooks not operating systemically is that an essential piece of data (usually, but not always retricted to the HHOOK) is not stored in a shared section, so each loaded instance of the DLL has a different value for the variable, and only one (in the address space of the initial loader app) has the right value and behaves as expected.

So, to create a shared section :

1. Use a pragma in your DLL code to specify the shared variables :

#pragma data_seg(".SHARDAT")
HWND hWndMain = 0;
HHOOK hKeyHook = NULL ;
HHOOK hWndHook = NULL ;
HHOOK hMsgHook = NULL ;
BOOL bSomeFlagOrOther = FALSE;
#pragma data_seg()

Note that shared variables *MUST* be initialised.

2. Create a DEF file with the following in it :

SECTIONS
.SHARDAT READ WRITE SHARED

Where the section name matches that in your pragma above. If having a
DEF file offends you, you can specify the section in a linker command
parameter (see the docs for the linker).

+ Recent posts