CompareFileEx Function  
 
BOOL WINAPI CompareFileEx(
  LPCTSTR lpszFile1,  
  LPCTSTR lpszFile2,  
  DWORD dwOptions,  
  LPCTSTR lpszDigest  
);

The CompareFileEx function compares the contents of two files with an optional message digest.

Parameters

lpszFile1
A null-terminated string which specifies the name of the first file to compare. This parameter must specify the name of a file which exists and can be opened for read access by the current process. If the file name specifies a directory or device name, the function will fail. This parameter cannot be NULL or a zero-length string. It is permitted to use environment variables in the file name by surrounding the variable name with the percent symbol. If the file name contains any leading or trailing space characters after the variable expansion, they will be removed.
lpszFile2
A null-terminated string which specifies the name of the second file to compare. If this parameter is NULL or a zero-length string, the lpszDigest parameter must specify a valid message digest and the contents of the first file will be compared against that digest. If this parameter specifies a file name, the file must exist and the current process must be able to open it for read access. If the file name specifies a directory or device name, the function will fail. It is permitted to use environment variables in the file name by surrounding the variable name with the percent symbol. If the file name contains any leading or trailing space characters after the variable expansion, they will be removed.
dwOptions
An unsigned integer value which specifies one or more option flags which will control how the files are compared. It may be one or more of the following values:
Value Description
COMPARE_FILE_DEFAULT Use the default comparison option, which is to perform a byte comparison of both files. If the lpszDigest parameter is not NULL or a zero-length string, the function will compare the message digest against the contents of the file. If the lpszDigest parameter has a value and no specific hash algorithm has been specified as one of the options, the hash type will be automatically determined based on its value
COMPARE_FILE_CONTENTS Perform a byte comparison of the two files. This option requires both files be specified. The current process must be able to open both files for exclusive read access. If either file is opened for write access, the function will fail. The lpszFile2 parameter must specify a valid file name if this option is used.
COMPARE_FILE_TIME_CREATED Compare the file creation times for both files and only consider them to be identical if the creation times are identical. If this option is specified and the creation times are different, the function will return zero even if the contents of the two files are identical. The lpszFile2 parameter must specify a valid file name if this option is used.
COMPARE_FILE_TIME_MODIFIED Compare the file modification times for both files and only consider them to be identical if the modification times are identical. If this option is specified and the modification times are different, the function will return zero even if the contents of the two files are identical. The lpszFile2 parameter must specify a valid file name if this option is used.
COMPARE_FILE_DIGEST_MD5 Use the MD5 algorithm to compute 128-bit hash value for the contents of the first file and compare this against the specified digest string. This algorithm is the most efficient method to compute a hash to verify data integrity, however it is not considered cryptographically secure and hash collision is possible. When this option is used the lpszDigest parameter must specify a valid MD5 hash as a 32 digit hexadecimal string.
COMPARE_FILE_DIGEST_SHA1 Use the SHA-1 algorithm to compute 160-bit hash value for the contents of the first file and compare this against the specified digest string. This algorithm is comparable to the MD5 algorithm and has a lower chance of hash collision; however, it is not considered cryptographically secure and hash collision is possible. When this option is used the lpszDigest parameter must specify a valid SHA-1 hash as a 40 digit hexadecimal string.
COMPARE_FILE_DIGEST_SHA256 Use the SHA-256 algorithm to compute a 256-bit hash value for the contents of the first file and compare this against the specified digest string. This is a cryptographically secure algorithm which is more compute intensive than either MD5 or SHA-1, but the chance of a hash collision is extremely low and typically not a concern for most applications. When this option is used the lpszDigest parameter must specify a valid SHA-256 hash as a 64 digit hexadecimal string.
lpszDigest
A null-terminated string which specifies a sequence of hexadecimal numbers which are used to compare against the computed hash value for the file. If this parameter is not NULL and no option is specified to indicate the hash type, it will be inferred from the length of the string. If this parameter is NULL or a zero-length string, it will be ignored. The case of the hexadecimal digits in the string are ignored and it is permitted, but not required, to prefix the string with either "0x" or "&H" to indicate it is a hexadecimal value. If the string does not specify a valid hexadecimal number or is not a value computed using one of the supported hash algorithms, the function will fail.

Return Value

A non-zero value is returned if the files are identical using the specified criteria. A value of zero indicates the files are different or an error has occurred. Check the value returned by the GetLastError function to determine the reason the file comparison was not successful. If the files are identical, this function will clear the last error code for the current thread.

Remarks

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.

Example

// Compare the contents of a file against an SHA-1 hash value
LPCTSTR lpszFile = _T("%USERPOFILE%\\Documents\\Projects\\MyDocument.docx");
LPCTSTR lpszSHA1 = _T("541E43FDC612E41C02B770A2FD87EBB4A12EA800");
BOOL bIdentical;

bIdentical = CompareFileEx(
                lpszFile,
                NULL,
                COMPARE_FILE_DIGEST_SHA1,
                lpszSHA1);

// Compare the contents of two files
LPCTSTR lpszFile1 = _T("%USERPROFILE%\\AppName\\ProjectData.db");
LPCTSTR lpszFile2 = _T("%TEMP%\\BackupData.db");

bIdentical = CompareFileEx(
                lpszFile1,
                lpszFile2,
                COMPARE_FILE_CONTENTS,
                NULL);

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csncdv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also