When I select an application that causes problems I see the error message (as expected).
But I also get an Access-Violation message.
So maybe I have to add the try block in TVersionInfo.ReadVersionInfo ?
Or should I modify TVersionInfo.ReadVersionInfo so when the size is zero, that I don't call RaiseLastOSError but show a message and exit?
The access violation probably comes from the fact that since, indirectly,
ReadVersionInfo is called from the constructor, an exception will prevent the constructor from finalizing, so your call to
VerInfo.Free catchs the object at an ... "incovenient" moment
To avoid it, you have some options but the best, IMHO, is to modify how TVersionInfo works. The needed changes are rather few and small:
- Change TVersionInfo.Create() to also set FBuffer to Nil.
- Delete the call to ReadVersionInfo from TVersionInfo.SetFileName.
- At the beginning of methods that use FBuffer (GetFixedFileInfo and GetVerValue) add this guard:
if FBuffer = Nil then ReadVersionInfo;
The basic effect of the changes is to defer doing anything until it is actually needed, instead of proactively doing it as soon as possible; which incidentally prevents raising exceptions until we are actually ready to catch them. Just a question of tastes
ETA Incidentally, if I were you I wouldn't call that class
TVersionInfo but
TAltVersionInfo or something like that, since there is already a class
TVersionInfo in the
Fileinfo unit.