Link to home
Create AccountLog in
C++

C++

--

Questions

--

Followers

Top Experts

Avatar of databoks
databoksπŸ‡©πŸ‡°

Volume Shadow Copy c++
Hi.

I want to use VSS to create a snapshot for a folder, file, volume etc.

I have downloaded Windows 7 SDK which also comes with some samples.
But these samples i cannot understand.

Is there anyone who can help me with a sample?

Im using MinGW with netbeans and i also have Visual Studio 2010.

Thanks.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of jkrjkrπŸ‡©πŸ‡ͺ

Check the article http://msdn.microsoft.com/en-us/library/windows/desktop/aa384589%28v=vs.85%29.aspxΒ ("Overview of Processing a Backup Under VSS") - it illustrates the source code examples (linked from there). Also see the 'big picture' at http://msdn.microsoft.com/en-us/library/windows/desktop/aa384629%28v=vs.85%29.aspxΒ ("Using the Volume Shadow Copy Service") and the tutorial at http://www.codeproject.com/Articles/321273/VSS-API-IN-CPPΒ ("Introduction to the Volume Shadow Copy Service API in C++"), the latter also comes with full source code.

Avatar of databoksdataboksπŸ‡©πŸ‡°

ASKER

jkr, i am glad having you on this post.

I have tried to follow the example on codeproject but VS comes with an error:

Avatar of databoksdataboksπŸ‡©πŸ‡°

ASKER

Here is the attachement
error.png

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of jkrjkrπŸ‡©πŸ‡ͺ

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of databoksdataboksπŸ‡©πŸ‡°

ASKER

I had fixed it i added vssapi.lib to as a link. Now i am getting another error when i run the program, i will try first to see if i can solve it myself.

will get back to you,

thanks, jkr.

Avatar of databoksdataboksπŸ‡©πŸ‡°

ASKER

I got this error in the Event Viewer:
Volume Shadow Copy Service error: A critical component required by the Volume Shadow Copy service is not registered. This might happened if an error occurred during Windows setup or during installation of a Shadow Copy provider. The error returned from CoCreateInstance on class with CLSID {e579ab5f-1cc4-44b4-bed9-de0991ff0623} and Name IVssCoordinatorEx2 is [0x80040154, Class not registered].
Operation:
Instantiating VSS server


So i found that is was building a 32 bit app on a x64 bit app which is not a good idea.

So for everyone change your project type in VS to x64 in the configuration manager.

I will get back to your later, Jkr regarding the post you posted.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of jkrjkrπŸ‡©πŸ‡ͺ

Building a 32bit app on a 64bit system is not really an issue, but *running* is ;o)

Avatar of databoksdataboksπŸ‡©πŸ‡°

ASKER

Yeah i know that, but when using VSS you have to build a differnt app foreach platform.

I took my very long time to finally find a way to backup a file. Microsoft Documentation on this(Volume shadow copy for Developers) is so lame that i don't want to use Windows anymore after i finished this application.

However i got it working and for all othe peops, here is the source. The only license is that you agree with me that microsoft is stupid sometimes, then you can distribute it for free :)
// nytest.cpp : Defines the entry point for the console application.
//
#include <comdef.h>
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	
	int retcode = 0;
	int i = 0;
	HRESULT hr;
	IVssEnumObject *pIEnumSnapshots;
	IVssBackupComponents *ab;
	VSS_OBJECT_PROP	Prop;
	VSS_SNAPSHOT_PROP& Snap = Prop.Obj.Snap;
	WCHAR existingFilePath[MAX_PATH] = TEXT("C:\\PageFile.sys");
	WCHAR newFileLocation[MAX_PATH] = TEXT("C:\\PageFileTEST.sys");
	TCHAR existingFileLocation[MAX_PATH];


	TCHAR sourceFile[_MAX_PATH] = TEXT("C:\\PageFile.sys");
	LPTSTR DestinationFile = TEXT("D:\\PageFileTEST.sys");


	if(CoInitialize(NULL) != S_OK)
	{

		return 2;
	
	}      // initialize security 
      if(FAILED(CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT_PRIVACY, 
         RPC_C_IMP_LEVEL_IDENTIFY,NULL,EOAC_NONE,NULL))) { 
      

         return(0); 
      } 

	hr = CreateVssBackupComponents(&ab);
	if(hr != S_OK)
	{
		
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 2;
	}

	hr = ab->InitializeForBackup();
	if(hr != S_OK)
	{
		
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		printf("erorr..");
		return 4;
	}

	

	IVssAsync *pAsync = NULL;
	hr = ab->GatherWriterMetadata(&pAsync);
	if(hr != S_OK)
	{
		
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 5;
	}
	hr = pAsync->Wait();
	if(hr != S_OK)
	{return 1;}

	pAsync->Release();


	VSS_ID snapshotID;
	hr = ab->StartSnapshotSet(&snapshotID);
	if(hr != S_OK)
	{

			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 6;
	}

	
	 VSS_ID SnapShotId; 

	
	
	WCHAR volumeName[4] = TEXT("C:\\");
	hr = ab->AddToSnapshotSet(volumeName,GUID_NULL,&SnapShotId);
	if(hr != S_OK)
	{
		
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 7;
	}

	hr = ab->SetBackupState(false,false,VSS_BT_FULL,true);
	if(hr != S_OK)
	{
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();
		return 1;

	}

	IVssAsync *pPrepare = NULL;
	hr = ab->PrepareForBackup(&pPrepare);
	if(hr != S_OK)
	{
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 8;
	}
	hr = pPrepare->Wait();
	if(hr != S_OK)
	{
		
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 9;
	}

	IVssAsync *pDoShadowCpy = NULL;
	hr = ab->DoSnapshotSet(&pDoShadowCpy);
	if(hr != S_OK)
	{
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();

		return 10;
	}
	hr = pDoShadowCpy->Wait();
	if(hr == S_OK)
	{
		VSS_SNAPSHOT_PROP snapshotprop = {0};
		hr = ab->GetSnapshotProperties(SnapShotId,&snapshotprop);
			_com_error error(hr);
		   LPCTSTR errorText = error.ErrorMessage();


		if(hr == S_OK)
		{
			int sourcelength = wcslen(snapshotprop.m_pwszSnapshotDeviceObject) + _tcslen(sourceFile);
			
			WCHAR test[260];
			wsprintf(test,L"%s%s",snapshotprop.m_pwszSnapshotDeviceObject,L"C:\\logFile.log");
			
			CopyFile(test,L"C:\\test.txt",false);
			//exit(1);
			
			
			
			LPTSTR sourcesnapshotfile = new TCHAR[sourcelength];
			_tcscpy_s(sourcesnapshotfile,sourcelength,snapshotprop.m_pwszSnapshotDeviceObject);
			_tcscat_s(sourcesnapshotfile,sourcelength,&sourceFile[2]);


			if(!::CopyFile(sourcesnapshotfile,DestinationFile,FALSE))
			{
				DWORD lastError = GetLastError();
			}
			else
			{
				printf("File copied..");
			}

			delete[] sourcesnapshotfile;
			VssFreeSnapshotProperties(&snapshotprop);
			pDoShadowCpy->Release();
			/*pPrepare->Release();*/
			LONG deletedsnapshotid = 0;
			VSS_ID nonDeletedSnapshotID;
			hr = ab->DeleteSnapshots(SnapShotId,VSS_OBJECT_SNAPSHOT,true,&deletedsnapshotid,&nonDeletedSnapshotID);
			if(hr != S_OK)
			{

				return 11;
			
			}
		}
	}
	





























	
	return 0;

}

	

Open in new window


I know the code can be trimmed and i some scenarios i have hardocoded some argumetns where noramlly you will put in variables Β such as Volume(C:\,D:\).

While using this please make sure that the shadows are deleted Β use vssadmin list shadows

Jkr, i believe you should have the points for the links you provided. I did not fix my problems, but it pointed me in some direction.

I really appreciate your help. Have a nice day.
C++

C++

--

Questions

--

Followers

Top Experts

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.