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

#include <windows.h>
#include "uData.h"
#include "DgHook.h"
//---------------------------------------------------------------------------
//   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
//---------------------------------------------------------------------------
//HINSTANCE ghInst;
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
//   ghInst = hinst;
   return 1;
}
//---------------------------------------------------------------------------
// 윈도우 훅 프로시저.
LRESULT CALLBACK WndHookProc(int Code, WPARAM wParam, LPARAM lParam)
{
   if(Code >= 0){
      CWPSTRUCT* lpWp = (CWPSTRUCT*)lParam;
      if(lpWp->message == WM_ACTIVATE){
         if((LOWORD(lpWp->wParam) != WA_INACTIVE) && (lpWp->hwnd != (HANDLE)GetData(tdRHandle)))
            SendMessage((HANDLE)GetData(tdRHandle), WM_USER + 100, (int)lpWp->hwnd, 0);
      }
   }
   return CallNextHookEx((HANDLE)GetData(tdWndHookID), Code, wParam, lParam);
}
//---------------------------------------------------------------------------
bool SetWndHook()
{
   HHOOK HWndHook;
//   HWndHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)WndHookProc, ghInst, 0);
   HWndHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)WndHookProc, NULL, GetCurrentThreadId());
   bool bRet = (bool)HWndHook;
   SetData(tdWndHookID, (int)HWndHook);
   return bRet;
}
//---------------------------------------------------------------------------
bool DeleteWndHook()
{
   HHOOK HWndHook;
   bool bRet = false;
   HWndHook = (LPVOID)GetData(tdWndHookID);
   if(HWndHook)
      bRet = UnhookWindowsHookEx(HWndHook);
   return bRet;
}
//---------------------------------------------------------------------------
// 키보드 훅 프로시저.
LRESULT CALLBACK KeybdHookProc(int Code, WPARAM wParam, LPARAM lParam)
{
   POINT Point;
   bool bRet;
   if(Code >= 0){   //wParam : 가상키 VK_PROCESSKEY = 229;, lParam : 스캔 코드값
      if(((wParam == 229) && (lParam == -2147483647)) ||
         ((wParam == 229) && (lParam == -2147483648.0))){
         GetCursorPos(&Point);
         if(GetData(tdRHandle) == (int)WindowFromPoint(Point)){
            bRet = true;
            return (int)bRet;
         }
      }
   }
   return CallNextHookEx((HANDLE)GetData(tdKeybdHookID), Code, wParam, lParam);
}
//---------------------------------------------------------------------------
bool SetKeybdHook()
{
   HHOOK HKeybdHook;
//   HKeybdHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeybdHookProc, ghInst, 0);
   HKeybdHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeybdHookProc, NULL, GetCurrentThreadId());
   bool bRet = (bool)HKeybdHook;
   SetData(tdKeybdHookID, (int)HKeybdHook);
   return bRet;
}
//---------------------------------------------------------------------------
bool DeleteKeybdHook()
{
   HHOOK HKeybdHook;
   bool bRet = false;
   HKeybdHook = (HANDLE)GetData(tdKeybdHookID);
   if(HKeybdHook)
      bRet = UnhookWindowsHookEx(HKeybdHook);
   return bRet;
}
//---------------------------------------------------------------------------

