The CompareFileEx function can be used to either compare the
contents of two files or compare the contents of a file against a
pre-computed hash value to determine if they match. In addition to
comparing file contents, the function can also check the
file creation and/or modification times to ensure they are the same.
You can use the GetMessageDigest function to compute a hash
value for a file.
In most cases, an application will specify either two files to
compare or a message digest value. If you call the function using
two file names and a message digest, the contents of both files
must be identical and the computed hash value must match the specified
message digest. If any of the requirements are not satisfied, the
function will return zero (false). For example, if the actual contents
of the two files are identical, but the value specified by
lpszDigest does not match the computed hash value of files, the
function will still return zero.
This function performs the comparison in a way to minimize potential
disk I/O whenever possible. First, the size of the two files are compared
and if they are different, no further checks are performed. Next,
if either the COMPARE_FILE_TIME_CREATED or COMPARE_FILE_TIME_MODIFIED
options have been specified, the file timestamps are compared and no
further checks are performed if they do not match. A byte comparison of
both files will only be performed after the other comparison criteria
has been satisfied.
Comparing the contents of a file against a message digest value is
the most expensive operation in terms of memory and processor
utilization. The larger the files are, and the more complex the hash
algorithm, the more compute-intensive the operation will be. In most
cases, the SHA-1 algorithm provides a good balance between resource
utilization and minimizing the possibility of hash collisions where
different files could potentially generate the same hash value.
If it is critically important to minimize the possibility of a hash
collision, use the SHA-256 algorithm; however, be aware of the impact
it can have on performance when used with very large files. The current
thread will block while the hash is being computed and if a large
amount of data is being processed, this may cause the application
to become non-responsive. In that case, it is recommended you create
a background worker thread and call this function from within that
worker thread, rather than inside the main UI thread.
To avoid potential problems with relative pathing in multithreaded
applications, it is recommended you use absolute paths whenever
possible. If you do not specify an absolute path, the file locations
will be considered relative to the current working directory for the
process. Because this is shared between all threads, it is possible
for one thread to change the current working directory at any time and
this can create inconsistent results. If you must use relative paths
in a multithreaded application, use a mutex or critical section to
ensure other threads cannot change the current working directory for
the process while performing the file comparison.
This function will normalize the file names provided by the caller
and perform checks to make sure the names do not contain illegal
characters. If the name includes quotes, wildcard characters or other
invalid symbols the function will fail and return zero with the last
error code set to ST_ERROR_INVALID_FILE_NAME. It is permitted to
use forward slashes in path names and these will be converted to the
standard backslash character used with Windows paths. This function
cannot be used to compare the contents of NTFS alternate data streams.
This function is defined using an alias which automatically selects
an ANSI or Unicode version based on the value of the UNICODE
preprocessor macro. If you use the ANSI version of this function,
the expanded file names will always be considered UTF-8 encoded
strings, regardless of the current locale of the application. If your
application uses file names which contain non-ASCII characters, you
should always use the Unicode version of the function, even if your
project is configured to use a multi-byte character set.