Link to home
Start Free TrialLog in
Avatar of all_rays
all_rays

asked on

KeyLogger Delphi

I have problem when using dll from http://www.freevbcode.com/ShowCode.Asp?ID=728, the author give an example in VB, when i try translate that code into Delphi, i found that my code doesn't work

could someone help me
thanks

this some part of my code

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public

    { Public declarations }
  end;

  function Hooked:boolean; external 'KTKbdHk.dll';
  function HookKeyboard(Hwnd,MsgID:Cardinal):LongInt; external 'KTKbdHk.dll';
  function UnhookKeyboard:Boolean; external 'KTKbdHk.dll';

var
  Form1: TForm1;

const
  MessageAris=$11;


implementation

{$R *.dfm}

type
{$IFDEF WIN32}
  WParameter = LongInt;
{$ELSE}
  WParameter = Word;
{$ENDIF}
  LParameter = LongInt;
{Declare a variable to hold the window procedure we are replacing}
var
  OldWindowProc : Pointer;

function NewWindowProc(WindowHandle : hWnd;
                       TheMessage   : WParameter;
                       ParamW       : WParameter;
                       ParamL       : LParameter) : LongInt;  stdcall;
var  tempRes      : integer;
     charRes      : char;
     keyboardState: TKeyboardState;
begin
{ Process the message of your choice here }
  if TheMessage = MessageAris then
  begin
   // The application should enter this area when i press some key
   // but it doesn't work, anyone know why?
  end;
  NewWindowProc := CallWindowProc(OldWindowProc,
                                  WindowHandle,
                                  TheMessage,
                                  ParamW,
                                  ParamL);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if Hooked then
  ShowMessage('Hooked');
 if HookKeyboard(Button3.Handle,MessageAris)<>0 then
  Button1.Enabled:=False;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 OldWindowProc := Pointer(SetWindowLong(Button3.Handle,GWL_WNDPROC,longInt(@NewWindowProc)));
 Button1.Click;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnhookKeyboard;
  SetWindowLong(Button3.Handle,GWL_WNDPROC,LongInt(OldWindowProc));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if UnhookKeyboard then
 Button1.Enabled:=True;
end;
Avatar of calinutz
calinutz
Flag of Romania image

Why not use the Windows API directly? Do you need it to be a dll?
Avatar of DavidBirch2dotCom
DavidBirch2dotCom

Try downloading this code - it should do what you want

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=7&txtCodeId=588

David
Avatar of all_rays

ASKER

for calinutz, what do you mean with Windows API? could you give me some example code
thanks
David, i have already download that link, but unfortunately it isn't what i want. actually i don't wont use timer anymore. i will be more apreciate if someone could correct my code above. thanks
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi Slick812, thanks for your answer. i think your library is better than what i have ever had before.
actually, i still confuse with windows hooking.
Could you help me to give some link or literature related with windows  hooking, please send to my email at belum5menit@yahoo.com

For this session i will give this point to u

thanks  
if you look in your windows API Help that comes with Delphi, for the index of word  "Hooks", there is info here about hooking, you will need to click the  >_>  arrows at the top to advance to the next pages of that muti-page explanation

at the About web site for delphi there is a page about hooks at

http://delphi.about.com/library/bluc/text/uc063001a.htm

this gives some of the basics for hooks, and there is code there to look at

you may also go to the MSDN library on the web and look for hook info there