How to: Synchronize Files by Using Unmanaged Code

This topic shows how to use an unmanaged language, such as C++, to create an application that synchronizes files and subfolders by using the Sync Framework file synchronization provider.

This topic assumes a basic familiarity with C++ and COM concepts.

The examples in this topic focus on the following Sync Framework interfaces:

Understanding File Synchronization

Sync Framework implements a synchronization provider that can be used to synchronize files and subfolders that are contained in a folder on a file system. This provider exposes several configurable settings to give a finer degree of control over exactly how synchronization occurs and which items are synchronized. To synchronize files between two folders, an application completes the following basic steps:

  1. Creates an IFileSyncProvider object to represent each folder.

  2. Passes the two providers to an ISyncSession object, and specifies one as the source provider and the other as the destination provider.

  3. Starts the synchronization session.

For more information about synchronizing files, see Synchronizing Files.

Build Requirements

  • Synchronization.h: declarations for Sync Framework core components.

  • FileSyncProvider.h: declarations for the file synchronization provider.

  • Synchronization.lib: import library for Sync Framework core components.

  • FileSyncProvider.lib: import library for the file synchronization provider.


The example code shows the following tasks:

  • How to create and initialize a source file synchronization provider and a destination file synchronization provider.

  • How to establish a filter to control which items are included in synchronization.

  • How to use a synchronization session to synchronize items between the folders that are represented by the providers.

This example uses an MFC (Microsoft Foundation Classes) dialog to obtain folder and filter information from the user. The strings that a user entered are packaged into CString objects, and these objects are then used to initialize the providers and filter.

Creating and Initializing a File Synchronization Provider

Create an IFileSyncProvider object by using CoCreateInstance.

The provider must be initialized before it can be used. This is also an opportunity to configure any settings. This implementation passes a filter, pFilter, to control which items are included in synchronization. The next section shows how to set up the filter.

Using a File Sync Scope Filter

Create a filter by using the IFileSyncProvider::CreateNewScopeFilter method. This new filter has no relationship to its creating provider. To connect a filter to a provider, pass the filter to the IFileSyncProvider::Initialize method. This means only one filter must be created per synchronization session, because the same filter can be passed to both providers. Be aware that the filter information is stored by the provider in the Initialize method. Therefore, later changes to the filter will not be reflected in the provider.

This implementation obtains filter option values from the user by using an MFC dialog. The dialog code is not shown. But the option values are stored in several CString objects, such as m_strFilenameExc. Our example sets the filter options to the values supplied by the user.

Starting Synchronization

Now that the providers and the filter have been configured and initialized, create and start a synchronization session.

Complete Code Example

The following code is the complete code for this example. The previous examples in this section were taken from this code.

See Also


Synchronizing Files

Sync Framework File Synchronization Components

Other Resources

IFileSyncProvider interface

IFileSyncScopeFilter interface