home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d123456 / DFS.ZIP / DFSKb.pas < prev    next >
Pascal/Delphi Source File  |  1998-11-25  |  4KB  |  163 lines

  1. {.$DEFINE DFS_DEBUG}
  2.  
  3. { Interface to the DFSKbMon.DLL library. }
  4. { Intended for use with the TDFSStatusBar component (DFSStatusBar.pas) }
  5.  
  6. unit DFSKb;
  7.  
  8. interface
  9.  
  10. uses
  11.   {$IFDEF DFS_DEBUG}
  12.   DFSDebug,
  13.   {$ENDIF}
  14.   Windows;
  15.  
  16. {
  17.   DFSKbDLLName contains the full filepath to the DLL to be loaded. It defaults
  18.      to just 'DFSKbMon.dll' so that the path is searched.  Change before calling
  19.      InitDFSKbDLL if you want to specify a location.
  20.   DFSKbDLL_Loaded indicates whether the DLL was loaded or not.
  21. }
  22. var
  23.   DFSKbDLLName: string;
  24.   DFSKbDLL_Loaded: boolean;
  25.  
  26. { Call before anything else to load the DLL and set up everything. }
  27. procedure InitDFSKbDLL;
  28. { Call if you want to manually unload the DLL.  Don't normally need since it
  29.   will do it automatically at app exit. }
  30. procedure UnloadDFSKbDLL;
  31. { Ask to be notified of Caps, Num, Scroll lock changes.  Return value is the
  32.   window message that will be sent to notify of change, or 0 if failed. }
  33. function DLLRegisterKeyboardHook(Handle: HWND): UINT;
  34. { Remove from notification list. }
  35. procedure DLLDeregisterKeyboardHook(Handle: HWND);
  36.  
  37. implementation
  38.  
  39. uses
  40.   Classes, SysUtils;
  41.  
  42. var
  43.   DLLRegisterKeyboardHookPtr: function (Handle: HWND): UINT; stdcall;
  44.   DLLDeregisterKeyboardHookPtr: procedure (Handle: HWND); stdcall;
  45.   hDFSKbDLL: THandle; { DLL handle }
  46.   RegisteredClients: TList;
  47.  
  48. { Load the DLL and get all the procedure addresses. }
  49. function LoadDFSKbDLL: boolean;
  50. var
  51.   OldMode: UINT;
  52. begin
  53.   {$IFDEF DFS_DEBUG}
  54.   DFSDebug.Log('DFSKb: LoadDFSKbDLL start', TRUE);
  55.   {$ENDIF}
  56.   if hDFSKbDLL <> 0 then
  57.     FreeLibrary(hDFSKbDLL);
  58.   OldMode := SetErrorMode(SEM_NOOPENFILEERRORBOX); { No system messages if can't load. }
  59.   hDFSKbDLL := LoadLibrary(PChar(DFSKbDLLName));
  60.   Result := hDFSKbDLL <> 0;
  61.   SetErrorMode(OldMode);
  62.   if not Result then exit;
  63.  
  64.   { Get all the function addresses }
  65.   @DLLRegisterKeyboardHookPtr := GetProcAddress(hDFSKbDLL, 'RegisterKeyboardHook');
  66.   @DLLDeregisterKeyboardHookPtr := GetProcAddress(hDFSKbDLL, 'DeregisterKeyboardHook');
  67.   {$IFDEF DFS_DEBUG}
  68.   DFSDebug.Log('DFSKb: LoadDFSKbDLL end', TRUE);
  69.   {$ENDIF}
  70. end;
  71.  
  72. { Procedure called when unit is finished, i.e. app exiting. }
  73. procedure CleanupDLL;
  74. var
  75.   x: integer;
  76. begin
  77.   {$IFDEF DFS_DEBUG}
  78.   DFSDebug.Log('DFSKb: CleanupDLL start', TRUE);
  79.   {$ENDIF}
  80.   if hDFSKbDLL <> 0 then
  81.   begin
  82.     // Paranoia check
  83.     if RegisteredClients.Count > 0 then
  84.     begin
  85.       {$IFDEF DFS_DEBUG}
  86.       DFSDebug.Log('DFSKb: Paranoia failed', TRUE);
  87.       {$ENDIF}
  88.       for x := RegisteredClients.Count-1 downto 0 do
  89.         DLLDeregisterKeyboardHook(HWND(RegisteredClients[x]));
  90.     end;
  91.  
  92.     {$IFDEF DFS_DEBUG}
  93.     DFSDebug.Log('DFSKb: attempting FreeLibrary', TRUE);
  94.     {$ENDIF}
  95.     if FreeLibrary(hDFSKbDLL) then
  96.     begin
  97.       {$IFDEF DFS_DEBUG}
  98.       DFSDebug.Log('DFSKb: unloaded DLL', TRUE);
  99.       {$ENDIF}
  100.       hDFSKbDLL := 0;
  101.       DFSKbDLL_Loaded := FALSE;
  102.     end;
  103.   end;
  104.   {$IFDEF DFS_DEBUG}
  105.   DFSDebug.Log('DFSKb: CleanupDLL end', TRUE);
  106.   {$ENDIF}
  107. end;
  108.  
  109. procedure InitDFSKbDLL;
  110. begin
  111.   DFSKbDLL_Loaded := LoadDFSKbDLL;
  112. end;
  113.  
  114. procedure UnloadDFSKbDLL;
  115. begin
  116.   CleanupDLL;
  117. end;
  118.  
  119. function DLLRegisterKeyboardHook(Handle: HWND): UINT;
  120. begin
  121.   if @DLLRegisterKeyboardHookPtr <> NIL then
  122.   begin
  123.     RegisteredClients.Add(Pointer(Handle));
  124.     Result := DLLRegisterKeyboardHookPtr(Handle);
  125.   end else
  126.     Result := 0;
  127. end;
  128.  
  129. procedure DLLDeregisterKeyboardHook(Handle: HWND);
  130. begin
  131.   if @DLLDeregisterKeyboardHookPtr <> NIL then
  132.   begin
  133.     RegisteredClients.Remove(Pointer(Handle));
  134.     DLLDeregisterKeyboardHookPtr(Handle);
  135.   end;
  136. end;
  137.  
  138.  
  139. initialization
  140.   {$IFDEF DFS_DEBUG}
  141.   DFSDebug.Log('DFSKb: init begin', TRUE);
  142.   {$ENDIF}
  143.   RegisteredClients := TList.Create;
  144.   DFSKbDLLName := 'DFSKbMon.dll';
  145.   hDFSKbDLL := 0;
  146.   {$IFDEF DFS_DEBUG}
  147.   DFSDebug.Log('DFSKb: init end.', TRUE);
  148.   {$ENDIF}
  149.  
  150. finalization
  151.   {$IFDEF DFS_DEBUG}
  152.   DFSDebug.Log('DFSKb: finalization begin.', TRUE);
  153.   {$ENDIF}
  154.   CleanupDLL;
  155.   RegisteredClients.Free;
  156.   {$IFDEF DFS_DEBUG}
  157.   DFSDebug.Log('DFSKb: finalization end.', TRUE);
  158.   {$ENDIF}
  159.  
  160. end.
  161.  
  162.  
  163.