-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVss_Writer-Test.cpp
More file actions
221 lines (195 loc) · 6.45 KB
/
Copy pathVss_Writer-Test.cpp
File metadata and controls
221 lines (195 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Vss_Writer-Test.cpp — Console VSS Writer that prints to stdout
// 30 September 2025
// Gilinsky
// Build: x64 (VS 2022+), Windows 10/11 SDK
// Linker -> Additional Dependencies: VssApi.lib; Ole32.lib; OleAut32.lib
//
// Run (elevated). If Initialize/Subscribe fails, try LocalSystem:
// psexec -s -i "C:\full\path\to\VssWriterConsole.exe"
//
// Test with Diskshadow (elevated):
// diskshadow
// DISKSHADOW> set context persistent
// DISKSHADOW> set verbose on
// DISKSHADOW> add volume C:
// DISKSHADOW> begin backup
// DISKSHADOW> writer verify
// DISKSHADOW> create
// DISKSHADOW> end backup
//
// Expected console output includes:
// Initialize hr=0x00000000
// Subscribe hr=0x00000000
// [Writer] OnPrepareBackup
// [Writer] OnPrepareSnapshot
// [Writer] OnFreeze (quiesce)
// [Writer] OnThaw (resume)
#include <windows.h>
#include <vss.h>
#include <vswriter.h>
#include <vsbackup.h>
#include <wchar.h>
#include <cstdarg>
#include <cstdio>
#include <io.h>
#include <fcntl.h>
#pragma comment(lib, "VssApi.lib")
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "OleAut32.lib")
static void PrintLn(const wchar_t* fmt, ...)
{
wchar_t buf[1024];
va_list ap; va_start(ap, fmt);
vswprintf_s(buf, _countof(buf), fmt, ap);
va_end(ap);
SYSTEMTIME st; GetLocalTime(&st);
wprintf(L"%04u-%02u-%02u %02u:%02u:%02u %s\n",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, buf);
fflush(stdout);
}
static void PrintHr(const wchar_t* what, HRESULT hr)
{
PrintLn(L"%s hr=0x%08X (gle=%lu)", what, (unsigned)hr, GetLastError());
}
// ----- VSS Writer -----
static const VSS_ID kWriterId =
{ 0x3d61b4e2, 0x6b17, 0x4e5c,{ 0x8c, 0x0b, 0x8a, 0xfb, 0x6f, 0x5e, 0x3f, 0x01 } };
class SampleWriter : public CVssWriter
{
protected:
// Advertise a simple FILEGROUP component under C:\ so requestors include us
bool OnIdentify(IVssCreateWriterMetadata* pMeta) override
{
// Some SDKs lack certain schema flags; set what’s available.
DWORD schemaMask = 0;
#ifdef VSS_BS_BASIC_BACKUP
schemaMask |= VSS_BS_BASIC_BACKUP;
#endif
#ifdef VSS_BS_VSS_COPY
schemaMask |= VSS_BS_VSS_COPY;
#endif
#ifdef VSS_BS_COPY
schemaMask |= VSS_BS_COPY;
#endif
#ifdef VSS_BS_INDEPENDENT_SYSTEM_STATE
schemaMask |= VSS_BS_INDEPENDENT_SYSTEM_STATE;
#endif
if (schemaMask != 0)
pMeta->SetBackupSchema((VSS_BACKUP_SCHEMA)schemaMask);
HRESULT hr = pMeta->AddComponent(
VSS_CT_FILEGROUP, // type
nullptr, // logical path
L"SampleComponent", // component name
L"SampleApp", // caption
nullptr, // icon
0, // icon size
false, // bRestoreMetadata
false, // bNotifyOnBackupComplete
true, // bSelectable
false // bSelectableForRestore
);
if (FAILED(hr)) { PrintLn(L"[Writer] OnIdentify AddComponent hr=0x%08X", (unsigned)hr); return false; }
hr = pMeta->AddFilesToFileGroup(
nullptr, // logical path
L"SampleComponent", // component
L"C:\\", // path
L"*", // filespec
true, // recursive
nullptr // alt location
);
if (FAILED(hr)) { PrintLn(L"[Writer] OnIdentify AddFiles hr=0x%08X", (unsigned)hr); return false; }
PrintLn(L"[Writer] OnIdentify (component advertised on C:\\)");
return true;
}
bool OnPrepareBackup(IVssWriterComponents* /*pComps*/) override
{
PrintLn(L"[Writer] OnPrepareBackup");
return true;
}
bool OnPrepareSnapshot() override
{
PrintLn(L"[Writer] OnPrepareSnapshot");
return true;
}
bool OnFreeze() override
{
PrintLn(L"[Writer] OnFreeze (quiesce)");
// TODO: flush/commit & pause app I/O; keep fast
return true;
}
bool OnThaw() override
{
PrintLn(L"[Writer] OnThaw (resume)");
// TODO: resume I/O
return true;
}
bool OnAbort() override
{
PrintLn(L"[Writer] OnAbort");
return true;
}
bool OnBackupComplete(IVssWriterComponents* /*pComps*/) override
{
PrintLn(L"[Writer] OnBackupComplete");
return true;
}
};
static volatile bool g_stop = false;
static BOOL WINAPI CtrlHandler(DWORD evt)
{
if (evt == CTRL_C_EVENT || evt == CTRL_BREAK_EVENT || evt == CTRL_CLOSE_EVENT) {
g_stop = true;
return TRUE;
}
return FALSE;
}
int wmain()
{
_setmode(_fileno(stdout), _O_U16TEXT); // unicode console (optional)
PrintLn(L"VSS Writer console starting (PID=%lu)", GetCurrentProcessId());
if (!SetConsoleCtrlHandler(CtrlHandler, TRUE)) {
PrintLn(L"SetConsoleCtrlHandler failed (gle=%lu)", GetLastError());
}
// COM init & security
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
PrintHr(L"CoInitializeEx", hr);
if (FAILED(hr)) return 1;
hr = CoInitializeSecurity(
nullptr, // default security descriptor
-1, // default auth services
nullptr, // auth services
nullptr, // reserved
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation
nullptr, // auth list
EOAC_DYNAMIC_CLOAKING, // capabilities
nullptr // reserved
);
PrintHr(L"CoInitializeSecurity", hr);
if (FAILED(hr) && hr != RPC_E_TOO_LATE) {
CoUninitialize();
return 1;
}
// Register writer
SampleWriter writer;
HRESULT hrInit = writer.Initialize(
kWriterId,
L"Sample VSS Writer (Console)",
VSS_UT_USERDATA,
VSS_ST_OTHER,
VSS_APP_BACK_END,
60000 // ms
);
PrintHr(L"Initialize", hrInit);
if (FAILED(hrInit)) { CoUninitialize(); return 1; }
HRESULT hrSub = writer.Subscribe();
PrintHr(L"Subscribe", hrSub);
if (FAILED(hrSub)) { CoUninitialize(); return 1; }
PrintLn(L"Writer subscribed. Press Ctrl+C to exit.");
// Keep alive for callbacks
while (!g_stop) Sleep(200);
PrintLn(L"Unsubscribing and exiting...");
writer.Unsubscribe();
CoUninitialize();
return 0;
}