Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Run applications with error message


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 leecan

leecan

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 05 December 2012 - 10:58 AM

Hello,


I have a Folder 'X' with number of application (test-1.exe, text-2.exe, ..., text-100.exe).

I want to create a code which will run all the application one by one in this folder, if the application cannot run for some error in the application, then a message will appear.

The following code can do this except the last part "Except"
Procedure RunApp (FileSource: String; Position_Change:Integer; old_Byte:String; New_Byte:String; EditClicktoRun: TEdit);
var
FileToRun: string;
i: cardinal;
begin
	 FileToRun := copy(FileSource ,1 , length(FileSource) - 4) + '_' + IntToStr(Position_Change) + '_' + (old_Byte) + 'TO' + (New_Byte) + '.exe'; // name of new application whci will run
	 Try
	 ShellExecute(0, 'Open', PChar(FileToRun), PChar(''), PChar(''), SW_NORMAL);
	 EditClicktoRun.Color := clred;
	 EditClicktoRun.Enabled := false;
	 sleep(200);
	 killtask(ExtractFileName(FileToRun));
    Except
	 on E : Exception do
		 begin
		 ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
		 end;[/b][/color]
	 End;
end;
what should be the last 4 lines code in order to show a message that there is error raised.

Thanks in advance,
Regards

#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts

Posted 05 December 2012 - 06:37 PM

Hello leecan,

I want to create a code which will run all the application one by one in this folder, if the application cannot run for some error in the application, then a message will appear.


Then you need to know how to get and handle error reported by the application. Basically there is no standarized or unified way for windows application to report error. If the applications were console application, you may want to monitor its StdErr channel (see this tutorial to understand what I am talking about).

If what you want is to detect failure of the execution of the executables, then you should detect the value returned by ShellExecute. If the value is lower than 32, then there was an error preventing the execution. Check Windows SDK for ShellExecute or visit this msdn page to get the meaning of the possible errors.

For example, you could do something like this:
  ...
  ...
  vExecResult := ShellExecute(0, 'open', PChar(FileToRun), nil, nil, SW_NORMAL);
  if vExecResult < 32 then
    case vExecResult of
      0: raise Exception.Create('Can not execute, system is out of memory or resources.');
      ERROR_FILE_NOT_FOUND: raise Exception.Create('Can not execute, file not found');
      ERROR_PATH_NOT_FOUND: raise Exception.Create('Can not execute, the path not found');
      ...
      else
        raise Exception.Create('Can not execute, unspecified error');
    end;

Btw, all exceptions derived from Exception class. So this:
  try
    ...
    ...
  except
    on E: Exception do blah blah
  end;

is the same with simpler code below.
  try
    ...
    ...
  except
    blah blah
  end;


#3 leecan

leecan

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 06 December 2012 - 06:52 AM

Thank you very much loutfi for your useful information.

If the applications were console application, you may want to monitor its StdErr channel (see this tutorial to understand what I am talking about).

Very interesting tutorial, I have to read it deeply.

At the moment, i have tried the code you provided:

vExecResult := ShellExecute(0, 'open', PChar(FileToRun), nil, nil, SW_NORMAL);
if vExecResult < 32 then
case vExecResult of
0: raise Exception.Create('Can not execute, system is out of memory or resources.');
ERROR_FILE_NOT_FOUND: raise Exception.Create('Can not execute, file not found');
ERROR_PATH_NOT_FOUND: raise Exception.Create('Can not execute, the path not found');
...
else
raise Exception.Create('Can not execute, unspecified error');
end;

But still i couldnt avoid the error : the application has encountered a problem and need to be closed.
I was unable to post the photo of the error.

Regards

#4 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts

Posted 07 December 2012 - 04:37 AM

First you have to note that the code was to detect the error that prevented the launching of a program. It will not detect/catch error raised in the application (when the application already running). To detect error raised within the application, you have to be familiar with the application to know how it reports errors.

If the application is a console/DOS program, you may be lucky that it may simply "writeln" error messages. In this case you either monitor StdErr or StdOut of the program (see the tutorial I mentioned earlier to find out how).

If the application(s) is not a console/DOS program, you might want to check if it uses system error log to report error, or perhaps they write to their own error log. In the latter case you can monitor the log file for changes and respond. If the applications report errors using modal dialog, you may want to monitor the existence of the modal dialog window class (and caption/title) and respond. The last approach however will not very reliable.

Edited by LuthfiHakim, 22 December 2012 - 10:34 AM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download