2010-05-26 4 views
6

Ich habe versucht, die Dokumentation für die API-Funktionen des Volumeschattenkopie-Dienstes zu lesen, um Dateien zu kopieren, die derzeit unter Windows XP gesperrt sind (verwendet werden).Volume Shadow Copy Service- (VSS-) Beispiel in C?

Leider scheint ich nirgendwo hin zu kommen. Hat jemand zufällig ein Codebeispiel für die Interaktion mit der API, um solche Dateien zu kopieren?

Danke, Doori Bar

Antwort

4

ich ähnliche Probleme gehabt haben, nach viel Hit-and-Versuche ich habe es geschafft, irgendwo zu bekommen ... hier ist ein Code-Schnipsel, die helfen können:

#include <stdio.h> 

#include <windows.h> 
#include <winbase.h> 

#include <Vss.h> 
#include <VsWriter.h> 
#include <VsBackup.h> 


int main() 
{ 
    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("\\temp\\PrinterList.txt"); 
    WCHAR newFileLocation[MAX_PATH] = TEXT("c:\\Users\\PrinterList.txt"); 
    TCHAR existingFileLocation[MAX_PATH]; 

    if (CoInitialize(NULL) != S_OK) 
    { 
     printf("CoInitialize failed!\n"); 
     return 1; 
    } 

    hr = CreateVssBackupComponents(&ab); 
    if(hr != S_OK) 
    { 
     printf("Failed at CreateVssBackupComponents Stage"); 
     return 1; 
    } 

    hr = ab->InitializeForBackup(); 
    if(hr != S_OK) 
    { 
     printf("Failed at InitializeForBackup Stage"); 
     return 1; 
    } 

    hr = ab->SetContext(VSS_CTX_ALL); 
    if(hr != S_OK) 
    { 
     printf("Failed at SetContext Stage"); 
     return 1; 
    } 

    hr = ab->Query(GUID_NULL,VSS_OBJECT_NONE, VSS_OBJECT_SNAPSHOT, &pIEnumSnapshots); 
    if(hr != S_OK){ 
     printf("Failed at Query Stage"); 
     return 1; 
    } 

    // Enumerate all shadow copies. 
    printf("Recursing through snapshots...\n"); 
    while(true) 
    { 
     // Get the next element 
     ULONG ulFetched; 
     hr = pIEnumSnapshots->Next(1, &Prop, &ulFetched); 

     // We reached the end of list 
     if (ulFetched == 0) 
      break; 

     wprintf(L"Snapshot:%s\n", Snap.m_pwszSnapshotDeviceObject); 
     /* 
     At this point you have the Snap object with all the information required for copying the file. 
     example: 
     Snap.m_pwszSnapshotDeviceObject is the root for snapshot object 
     (like \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1) 
     Snap.m_pwszOriginalVolumeName is the full unicode name 
     (like \\?\Volume{1240872a-88de-11df-a94d-806e6f6e6963}\) 
     for the original root(c: mostly) 

     So, you can use CopyFile() to do what you want 
     */ 


     wcscpy(existingFileLocation, Snap.m_pwszSnapshotDeviceObject); 
     wcscat(existingFileLocation, existingFilePath); 
     CopyFile(existingFileLocation, newFileLocation, false); 
     //false here will enable over-write 
    } 
    return retCode; 
} 

Hinweis: Wenn Ihr Zielcomputer nicht mit dem Buildcomputer identisch ist, müssen Sie die richtigen Definitionen und Buildkonfigurationen angeben.

Hinweis: Sie müssen die lästigen wchars überall verwenden, weil diese Funktionen sie verwenden.

+0

Entschuldigung für die späte Antwort, ich wollte Ihnen für Ihre Probe danken - ich hoffe, es wird anderen helfen, weil es nicht wie C aussieht? –

+0

Tatsächlich ist es C, einige C++ - Stildefinitionen sind irreführend. Ich werde es bearbeiten und es morgen zum Standard C machen (eine Frist, um die ich mich heute kümmern muss). – lalli

+0

Hier geht es, es ist meist C jetzt (mit win32 natürlich, für die API benötigt) ... – lalli