Share via


How to: Create an Unmanaged Synchronization Application

This topic shows how to use an unmanaged language, such as C++, to create an application that uses Microsoft Sync Framework to create a synchronization session and connect it to source and destination providers.

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

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

Understanding Synchronization Applications

A synchronization application is a software component that creates a synchronization session, connects it with two synchronization providers, uses the session to start synchronization, and hosts Sync Framework during synchronization.

For more information about the role of the synchronization application, see Implementing a Synchronization Application.

Build Requirements

  • Synchronization.h: declarations for Sync Framework components.

    #include <synchronization.h>
    
  • SynchronizationErrors.h: custom error codes.

    #include <synchronizationerrors.h>
    
  • Synchronization.lib: import library.

Example

The example code in this topic shows how to create a synchronization session, initialize it with source and destination providers, and start the session. The example also shows one way to display session statistics to the user.

Be aware that an application can obtain the providers in the way that is most appropriate for the kind of the application. The example assumes the application already has two ISyncProvider Interface interfaces: one that represents the source replica and the other that represents the destination replica.

Creating and Starting a Synchronization Session

The examples uses CoCreateInstance to create an IApplicationSyncServices object. This object is then used to create and initialize a session object. The session is started. When synchronization finishes, the session statistics are shown to the user in a message box.

HRESULT Synchronize(ISyncProvider* pProvSrc, ISyncProvider* pProvDest)
{
    HRESULT hr = E_UNEXPECTED;

    IApplicationSyncServices* pSvc = NULL;
    hr = CoCreateInstance(CLSID_SyncServices, NULL, CLSCTX_INPROC_SERVER, 
        IID_IApplicationSyncServices, (void**)&pSvc);
    if (SUCCEEDED(hr))
    {
        ISyncSession* pSession = NULL;
        hr = pSvc->CreateSyncSession(pProvDest, pProvSrc, &pSession);
        if (SUCCEEDED(hr))
        {
            SYNC_SESSION_STATISTICS syncStats;
            // Arbitrarily choose "destination wins" conflict resolution.
            hr = pSession->Start(CRP_DESTINATION_PROVIDER_WINS, &syncStats);
            if (SUCCEEDED(hr))
            {
                // Display the session statistics to the user.
                CString strMsg;
                strMsg.Format(L"%d changes succeeded.\n%d changes failed.", syncStats.dwChangesApplied,
                    syncStats.dwChangesFailed);
                MessageBox(NULL, strMsg.GetString(), L"Synchronization Statistics", MB_ICONINFORMATION);
            }

            pSession->Release();
        }

        pSvc->Release();
    }

    return hr;
}

Next Steps

Now that you have created a synchronization application, you might want to create a synchronization provider. For more information, see How to: Create an Unmanaged Synchronization Provider. You might also want to enhance the application by registering it to receive notifications during the session. For more information, see Responding to Synchronization Events.

See Also

Reference

ISyncSession Interface
SYNC_SESSION_STATISTICS Structure

Concepts

Implementing a Synchronization Application