Click here to Skip to main content
15,348,004 members
Articles / Desktop Programming / Win32
Posted 30 Jan 2012


32 bookmarked

Introduction to the Volume Shadow Copy Service API in C++

Rate me:
Please Sign up or sign in to vote.
4.85/5 (19 votes)
30 Jan 2012CPOL7 min read
Introduction to Volume Shadow Copy API in C++


The Volume Shadow Copy Service aka VSS provides the backup infrastructure for the Microsoft Windows operating system, as well as a mechanism for creating consistent point-in-time copies of data known as shadow copies. VSS was introduced in Windows XP SP2 and is available on Windows Server 2003, Vista, Windows Server 2008, Windows 7, and Windows Server 2008 R2.

The Volume Shadow Copy Service can produce consistent manual or automatic backup copies or snapshots of data, even if it has a lock by coordinating with applications, file-system services, backup applications, fast-recovery solutions, and storage hardware. Several features in the Windows OS make use the Volume Shadow Copy Service. Beginning with Vista, some components like the property sheet shell extension called Previous Versions allows restoring individual files or folders locally from the restore point, as they existed at the time of the snapshot, thus retrieving an earlier version of a file or recovering a file deleted by mistake.

What you will find in this article is how to create a basic snapshot using the VSS API in native C++. The MSDN documentation is very good but also a little bit confusing to say the least this is why I get the idea to write this article hoping it will expose the VSS API in a more friendly way. <o:p>

VSS Vocabulary

Before we write some code we have to identify the different components that make a solution:<o:p>

  • VSS service

This component is the part of Windows that ensures the other components can communicate with each other properly and work together.<o:p>

  • VSS requester

This is what this article will focus on. It is a just the set of instruction that creates, imports or deletes the shadow copies. A typical example is a backup application like NTBackup. Under vista or Win 7 and Win 2008, the Windows Server Backup utility and the System Center Data Protection Manager application are VSS requesters. This first installment will focus on the basics of VSS requesters. <o:p>

  • VSS writer

A VSS writer is a software component that guarantees that we have a consistent data set to back up by receiving freeze and thaw notifications to ensure that backup copies of their data files are internally consistent. SQL Server® or Exchange Server users are familiar with VSS writers as this two applications come with their own writer. Windows also include some VSS writers for various components like the registry for example. You would create your own VSS writer for your application if you want to guarantee data consistency during back up. The part 2 of this article will address VSS writers.<o:p>

  • VSS provider


This component creates and maintains the shadow copies. This can occur in the software or in the hardware. The Windows operating system includes a VSS provider that uses copy-on-write. If you use a storage area network (SAN), it is important that you install the VSS hardware provider for the SAN, if one is provided. A hardware provider offloads the task of creating and maintaining a shadow copy from the host operating system. I will refer to the VSS provider through this article but to know more about it, visit the MSDN page.

How does it operate?

Now that we know what a requester, a writer and a provider are, how about putting their roles into context and show how they interact with each other to create a snapshot?

Regardless of the specific purpose for the copy and the application making use of VSS, shadow copy creation follows the same steps:<o:p>

  1. The requester asks the Volume Shadow Copy Service to enumerate the writers, gather the writer metadata, and prepare for shadow copy creation.
  2. Each writer creates an XML description of the components and data stores that need to be backed up and provides it to the Volume Shadow Copy Service. The writer also defines a restore method, which is used for all components. The Volume Shadow Copy Service provides the writer's description to the requester, which selects the components that will be backed up. This part will be covered in the part 2.
  3. The Volume Shadow Copy Service notifies all the writers to prepare their data for making a shadow copy.
  4. Each writer prepares the data as appropriate, such as completing all open transactions, rolling transaction logs, and flushing caches. When the data is ready to be shadow-copied, the writer notifies the Volume Shadow Copy Service.
  5. The writers temporarily freeze application write I/O requests for the few seconds that are required to create the shadow copy of the volume or volumes. Note that during that period, the read I/O requests are still possible. The application freeze is not allowed to take longer than 60 seconds. The Volume Shadow Copy Service flushes the file system buffers and then freezes the file system, which ensures that the file system metadata is recorded correctly and the data to be shadow-copied is written in a consistent order.
  6. The shadow copy is then created by the provider. This operation lasts no more than 10 seconds, during which all write I/O requests to the file system remain frozen.
  7. The VSS Service releases file system write I/O requests and tells the writers to thaw application write I/O requests.











Writing some code

Now it’s time to dig in the API. I assume that you are aware of my development environment. VSS is available in the Microsoft Windows SDK. If you are using Windows Server 2003 and Windows XP then you must use the Volume Shadow Copy Service 7.2 SDK. There is a limitation in Windows XP; the snapshot created is not persistent. For this article the code have been compiled using Visual C++ 2010 and the windows SDK 7.1 and tested under windows Vista and windows 7. One more thing to know is that all VSS applications (requesters, providers, and writers) must run as native 32-bit or 64-bit applications. Running them under WOW64 is not supported.

The VSS API assumes knowledge of COM (intermediate).<o:p>

The interface for a home-brew requester: IVssBackupComponents

Whether you want to run a backup a restore or get some information about snapshots you will have to implement a requester. All requesters use the interface IVssBackupComponent. An application uses an instance of this interface to poll writer poll writers about file status and to run backup or restore operations. Applications obtain an instance of the IVssBackupComponents interface by calling CreateVssBackupComponents.

 // Create the IVssBackupComponents Interface
IVssBackupComponents *pVssBackup = NULL;
HRESULT hr = CreateVssBackupComponents(&pVssBackup);
if (result = S_OK) {. . .

Creating a snapshot

Any shadow copy creation will always require the methods IVssBackupComponents::InitializeForBackup and IVssBackupComponents::StartSnapshotSet.

// sets the context for subsequent shadow copy-related operations.
HRESULT result = pVssBackup->InitializeForBackup(); 
if (result == S_OK){
                    . . 

Even though there are different types of shadow copy a requester can create, most backup applications, would call the following methods:

1. Call IVssBackupComponents::SetContext

with a context of VSS_CTX_BACKUP.

 // sets the context for subsequent shadow copy-related operations.
HRESULT result = pVssBackup->SetContext(VSS_CTX_BACKUP | VSS_CTX_CLIENT_ACCESSIBLE_WRITERS                                        | VSS_CTX_APP_ROLLBACK);
if (result == S_OK) {. . . 
2. Call IVssBackupComponents::GatherWriterMetadata to initialize communication with writers.
// sets the context for subsequent shadow copy-related operations.
<pre>IVssAsync *pAsync = NULL;
HRESULT result = pVssBackup->GatherWriterMetadata(&pAsync);
if (result == S_OK) 
// Free all resources allocated during the call to GatherWriterMetadata

3. IVssBackupComponents::StartSnapshotSet to initialize the shadow copy mechanism.

VSS_ID snapshotSetID;
HRESULT result = pVssBackup->StartSnapshotSet(); 
if (result == S_OK) {. . . 

The VSS_ID definition specifies the general VSS identifier format. It is a standard GUID data type and used to identify shadow copies, shadow copy sets, providers, provider versions, writers.

4. Call IVssBackupComponents::AddToSnapshotSet to include volumes in the shadow copy.
HRESULT result = pVssBackup->AddToSnapshotSet(szVolumeName , GUID_NULL, &snapshotSetID); 
if (result == S_OK) {. . .  

The first parameters can be the name of the volume or the UNC path of the remote file share to be shadow copied. It must be in one of the following formats and must include a trailing backslash (\):

  • The path of a mounted folder, for example, Y:\MountX\
  • A drive letter, for example, D:\
  • A volume GUID path of the form \\?\Volume{GUID}\ (where GUID identifies the volume)
  • A UNC path that specifies a remote file share, for example, \\Clusterx\Share1\

5. Call IVssBackupComponents::PrepareForBackup to notify writers of backup operations.

IVssAsync *pAsync = NULL; 
HRESULT result = pVssBackup->PrepareForBackup(&pAsync); 
if (result == S_OK) {. . . 

The demo project attached to this article creates contains the code that creates a snapshot.

Working with snapshots

Windows XP and later include a command line utility called vssadmin that can be used to manipulate shadow copies. This utility requires administrator privileges.

The command vssadmin list shadows /for=c:\ will return the list of all the shadow copies on the drive C:\

On vista and Vista and higher, these copies can be mounted in Window explorer by typing the command:

mklink /d c:\mount_shadow_copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopyX\ . X is any number return by the previous command

You want to mount a shadow copy programmatically? Well the function CreateSymbolicLink is your answer. An example is commented out in the code that comes with this article. Note that this function is only available on Vista and Win7 and Win 2k8.

Coming up

This concludes the first part of the VSS series. I’ll be back with another article on advanced requester and VSS Writers. Till then happy coding.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
I have a passion for native and managed languages programming. I also like database administration/development.

Comments and Discussions

General1 star for this unfinished general article Pin
dc_200019-Oct-18 14:45
Memberdc_200019-Oct-18 14:45 
QuestionRestore workflow Pin
Member 132368244-Jun-17 22:04
MemberMember 132368244-Jun-17 22:04 
Questionexcellent article Pin
arpit280329-Jul-16 7:36
Memberarpit280329-Jul-16 7:36 
GeneralIt is neat and simple explanation Pin
paul crescent24-Sep-14 2:13
Memberpaul crescent24-Sep-14 2:13 
GeneralMy vote of 4 Pin
AdityaDange11-Feb-14 23:21
MemberAdityaDange11-Feb-14 23:21 
QuestionMy Snapshot Conflicts with system's restore points Pin
taozhicheng19-Sep-12 22:49
Membertaozhicheng19-Sep-12 22:49 
AnswerRe: My Snapshot Conflicts with system's restore points Pin
didierjeanphi24-Sep-12 22:41
Memberdidierjeanphi24-Sep-12 22:41 
QuestionThanks and.... Pin
Peter Weyzen3-Sep-12 18:48
MemberPeter Weyzen3-Sep-12 18:48 
AnswerRe: Thanks and.... Pin
didierjeanphi17-Sep-12 23:30
Memberdidierjeanphi17-Sep-12 23:30 
QuestionTaking backup of File and folders Pin
jude_aj30-Apr-12 1:43
Memberjude_aj30-Apr-12 1:43 
AnswerRe: Taking backup of File and folders Pin
didierjeanphi30-May-12 7:07
Memberdidierjeanphi30-May-12 7:07 
GeneralMy vote of 5 Pin
liyi_whu30-Mar-12 4:48
Memberliyi_whu30-Mar-12 4:48 
GeneralRe: My vote of 5 Pin
didierjeanphi30-May-12 7:00
Memberdidierjeanphi30-May-12 7:00 
GeneralMy vote of 5 Pin
heresmadison10-Feb-12 17:20
Memberheresmadison10-Feb-12 17:20 
GeneralMy vote of 5 Pin
Orkblutt7-Feb-12 12:11
MemberOrkblutt7-Feb-12 12:11 
GeneralRe: My vote of 5 Pin
didierjeanphi30-May-12 7:08
Memberdidierjeanphi30-May-12 7:08 
GeneralRe: My vote of 5 Pin
Orkblutt23-Nov-12 0:22
MemberOrkblutt23-Nov-12 0:22 
GeneralMy vote of 5 Pin
mier6-Feb-12 22:19
Membermier6-Feb-12 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.