The hook is more like an interrupt..
I guess the variable txtfile is not available (out of DLL scope)
in the HookProc
Because it is not initialized in the HookProc (and only
in the EnableHook proc) and the global DLL variable txtfie maybe
can't be reached while the hook is called
This is only a guess I can't test it at this place. But maybe it helps
You should try to define and it the file variable locally, and open and
close the file
at every write. like this :
library sysHook;
uses
Windows, Messages, SysUtils;
var
Hook : HHOOK;
function HookProc(nCode, wParam, lParam : integer) : LRESULT; stdcall;
var
txtfile : text; // Shouldn't this be texfile in Delphi ? or at least in Delphi 2.0 and later ???
Buf, FName : PChar;
i : integer;
hMod : THandle;
begin
if (nCode = HSHELL_WINDOWCREATED) then
begin
Result := 1;
buf := strAlloc(255);
hMod := GetClassLong(wParam, GCL_HMODULE);
GetModuleFileName(hMod,buf, 255);
Assignfile(Txtfile, 'C:CREATE.TXT');
Append(TxtFile);
WriteLn(TxtFile,StrPas(buf)+' Opened1');
flush (txtFile);
close (txtfile);
MessageBox(wParam, buf,buf,0);
strDispose(buf);
exit;
end;
Result := CallNextHookEx(0, nCode, wParam, lParam);
end;
procedure EnableHook; stdcall;
var
txtfile : text;
begin
Hook := SetWindowsHookEx(WH_SHELL, @HookProc, hInstance, 0);
Assignfile(Txtfile, 'C:CREATE.TXT');
Append(TxtFile);
Writeln(TxtFile, 'Hook Installed');
flush (txtfile);
Close (txtfile);
end;
procedure DisableHook; stdcall;
var
txtfile : text;
begin
UnhookWindowsHookEx(Hook);
Assignfile(Txtfile, 'C:CREATE.TXT');
Append(TxtFile);
Writeln(TxtFile, 'Hook Removed');
flush (txtfile);
CloseFile(TxtFile);
end;
exports
EnableHook,
DisableHook;
begin
end. |
|