【问题标题】:Volume Shadow Copy Service (VSS) sample in C?C 中的卷影复制服务 (VSS) 示例?
【发布时间】:2011-02-24 02:12:12
【问题描述】:

我一直在尝试阅读有关卷影复制服务的 API 功能的文档,目的是复制当前在 Windows XP 下被锁定(正在使用)的文件。

不幸的是,我似乎一无所获。任何人碰巧有一个如何与 API 交互以复制此类文件的代码示例?

谢谢,Doori 酒吧

【问题讨论】:

    标签: c windows-xp


    【解决方案1】:

    我也遇到过类似的麻烦,经过多次尝试和尝试,我终于找到了某个地方…… 这是一个可能有帮助的代码 sn-p:

    #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;
    }
    

    注意:如果您的目标机器与构建机器不同,您需要包含正确的定义和构建配置。

    注意:您将不得不在任何地方使用烦人的 wchars,因为这些函数使用它们。

    【讨论】:

    • 抱歉回复晚了,我想感谢您提供的样本 - 我希望它对其他人有所帮助,因为它看起来不像 C?
    • 其实是 C,一些 C++ 风格的定义会产生误导。明天我将对其进行编辑并使其成为标准 C(今天有截止日期)。
    • 你忘记给CoUninitialize()打电话了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 2011-03-02
    相关资源
    最近更新 更多