1

I'm using a TOpenPictureDialog to browse for images. In debug mode, when I'm browsing in this dialog for a picture and I happen to click (not double click) on a shortcut (.lnk), the debugger interrupts my program because it catches an exception, saying it's not a valid file format.

How to overcome this? I know it's only in debug time and don't have any issue in the final EXE, but it is getting very annoying, because I'd like to be able to go through these shortcuts.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • You say this happens while debugging, so you obviously have the debugger *right there*. Use it to track down the source of the problem. Pause the program and look at the call stack to find out who's reporting the error and what conditions were evaluated to decide that the format wasn't valid. (I *suspect* I know the cause already, but you've got a debugger in front of you and I don't.) – Rob Kennedy Dec 14 '11 at 17:44
  • I know the cause of the problem, I don't want to modify the dialog's code or anything, the problem is that this dialog is intended to preview an Image when you select it, but regardless of what you may have in the extension filter, it will still show shortcuts (.lnk) which when clicked (not attempting to open them even) it complains that it's not a valid image. – Jerry Dodge Dec 14 '11 at 17:49
  • I'm sure many have faced this question and that there must be some property to set or something. – Jerry Dodge Dec 14 '11 at 17:50
  • Changing the question from designtime to when debugging rather changed the complexion of the question! ;-) – David Heffernan Dec 14 '11 at 21:15
  • @DavidHeffernan Yes that was a stupid mistake of mine, mixed up terms – Jerry Dodge Dec 14 '11 at 21:19

1 Answers1

6

You are out of luck here, this bug was reported in the QC 69533 and was fixed in the update 3 of Delphi 2009.

The code used to by the VCL to verify if a file is valid image, doesn't check for the shortcuts files (.lnk) so the VCL thinks which the file is a valid image and try to load the file and then raises a EInvalidGraphic exception.

The exception is only raised in the debugger because a code like this is used to check the validate the filename.

(Only showing part of the real code because is VCL code)

ValidPicture := FileExists(FullName) and ValidFile(FullName);
if ValidPicture then
try
 // here try to load the file even if is a shortcut(.lnk)

except //this exception is caught by the debugger.
  ValidPicture := False; 
end;

Workarounds

1) You can add the EInvalidGraphic exception, to the exceptions list to ignore list.

enter image description here

2) you can write a detour (here you have a sample) and implement your own TOpenPictureDialog.DoSelectionChange method (validating the .lnk files), because is here where is made the validation of the files to load.

3) you can override the DoSelectionChange method of the TOpenPictureDialog using a interposer class, to validate the files to load.

  TOpenPictureDialog= class (ExtDlgs.TOpenPictureDialog)
    procedure DoSelectionChange; override;
  end;
Community
  • 1
  • 1
RRUZ
  • 134,889
  • 20
  • 356
  • 483