Ahh, further clues! Sounds like there might be a locking issue. When you open it for calculating the hash, do you use fmOpenRead and fmShareExclusive? If so, perhaps changing it to fmShareDenyWrite would do the trick?

At 10:01 AM 6/26/2009, Rob Cameron wrote:
Sid and Sub-zero,

Thanks for the further thoughts. It has been driving me nuts but I *think* I have semi-resolved it and so am (half-)shamefacedly reporting back.

The problem does not arise in the calculation of the hash, or the encryption/decryption per se. I think - think! - it is due to a difference in the way XP and Vista behave when Windows Explorer is open in a window behind my app. But I cannot reliably reproduce this with a new app.

Normally my app is launched from a desktop icon, or during development, from the Delphi IDE. There is a TOpenDialog to select the file to be processed. This works fine on both XP and Vista. However, for no good reason, when I tested the latest version on Vista, I happened to have Windows Explorer open (or minimized) and displaying the folder where the target file was stored while I was running the app. After restructuring my code I managed to display an error message from Windows saying that the "operation cannot be performed on a file with a user-mapped section open" or words to that effect.

My bad: I had a try ... finally clause that did not properly catch and display this error. :-(

This was before I was attempting to compute the hash. Curiously, it didn't seem to mind decrypting the file, so I had assumed that the process had finished correctly and was merely calculating the hash wrongly. (Maybe the low-level file I/O in Delphi doesn't trigger the error, while an operation that uses the Windows API does?) The reason I was getting different hash values was because the error kicked in before stripping out some extra bytes tagged on to the end of the file during the encryption process - I was calculating the hash for different file lengths (sorry!).

The reason why I was getting variable results with other files is that I (randomly) did or did not have Explorer open at the relevant folder at the time my app was executing. Now I have figured that out, I can repeatedly get good/bad behaviour.

So now I have been trying to precisely identify which operations are causing the problem.

After spinning in many small circles for a while, and logging every step of the processes, I am pretty sure that this explains the discrepancy, however despite hours of work I have been unable to pin it down to one particular function or line of code. Just when I think I have identified the bug by commenting out code line-by-line, I then fail to reproduce it in a simple, isolated test app.

Despite a lot of comparisons I have not seen this happen with XP: the app runs correctly with and without Explorer open at the target folder. With Vista, the app runs OK with Explorer closed, or open at a different folder, but fails with Explorer open at the target folder. It is not necessary to have the name of the target file in focus for my app to fail.

Which raises a question: is it possible to detect in advance whether another application has or has not freed a file for other use?

Thanks again for the helpful suggestions and sorry if I misled you about the nature of the problem.

Rob


------------------------------
Message: 4
Date: Thu, 25 Jun 2009 12:04:48 +0300
From: Fastream Technologies <[email protected]>
Subject: Re: Computing the SHA1 hash of a file: diffreent in XP and
        Vista?
To: Delphi-Talk Discussion List <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8
Why don't you try my ICS based code at:
http://www.fastream.com/md5extractor.php
Best Regards,
SubZero
On Wed, Jun 24, 2009 at 9:02 PM, Rob Cameron <[email protected]> wrote:

I wrote a program (in Delphi 7) and have been using it for ages to encrypt
then decrypt an executable file, then compare the hash of the decrypted file
with the original. Specifically:

Compute the hash of a file, save it. Let's call it "OldHash"
Encrypt the file in place
Decrypt the file in place
Compute the hash of the file, let's call it "NewHash"
Compare OldHash and NewHash.

In XP (SP3 now, but all SPs AFAIK): OldHash = NewHash.
In Vista: OldHash <> NewHash.

In both cases the decrypted file appears to be in good order - i.e. it
executes OK and shows no practial differences.

To make the comparisons, I have prepared a folder which contains only the
Launcher (which does the encrypt/decrypt) the target exe file and a couple
of files which are needed for the exe to run (they are, in fact untouched in
these tests). Then I copy this folder from a machine running XP to a machine
running Vista Business and get different results.

Any ideas?

Many thanks,

Rob
__________________________________________________
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi-talk

Regards,
Sid Gudes
PIA Systems Corporation
[email protected]

__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi-talk

Reply via email to