我正在尝试通过 IMAPI2 创建光盘映像。代码非常简单:
#include <windows.h>
#include <iostream>
#include <fstream>
#include <shlwapi.h>
#include <cstdio>
#include <imapi.h>
#include <imapi2.h>
#include <imapi2fs.h>
#include <imapierror.h>
#include <imapi2error.h>
#include <imapi2fserror.h>
using namespace std;
int main() {
CoInitialize(NULL);
HRESULT res = 0;
IFileSystemImage* image = NULL;
IFileSystemImageResult* result = NULL;
IFsiDirectoryItem* root = NULL;
IStream* file = NULL;
IStream* r_i = NULL;
res = CoCreateInstance(CLSID_MsftFileSystemImage, NULL, CLSCTX_ALL, __uuidof(IFileSystemImage), (void**) &image);
printf("1 %08X\n", res);
res = image -> put_FileSystemsToCreate((FsiFileSystems)(FsiFileSystemJoliet | FsiFileSystemISO9660));
printf("2 %08X\n", res);
res = image -> put_VolumeName(L"Pictures");
printf("3 %08X\n", res);
res = image -> ChooseImageDefaultsForMediaType(IMAPI_MEDIA_TYPE_CDRW);
printf("4 %08X\n", res);
res = image -> get_Root(&root);
printf("5 %08X\n", res);
res = root -> AddTree(L"C:\\pictures", VARIANT_TRUE);
printf("6 %08X\n", res);
res = SHCreateStreamOnFileEx(L"C:\\main.cpp", STGM_READ | STGM_SHARE_DENY_NONE | STGM_DELETEONRELEASE, FILE_ATTRIBUTE_NORMAL, false, NULL, &file);
printf("7 %08X\n", res);
res = root -> AddFile(L"main.cpp", file);
printf("8 %08X\n", res);
res = image -> CreateResultImage(&result);
printf("9 %08X\n", res);
res = result -> get_ImageStream(&r_i);
printf("0 %08X\n", res);
STATSTG stg;
r_i -> Stat(&stg, 1);
char* data = new char[stg.cbSize.QuadPart];
ULONG junk;
r_i -> Read(data, stg.cbSize.QuadPart, &junk);
FILE* f = fopen("image.iso", "wb");
fwrite(data, stg.cbSize.QuadPart, 1, f);
fclose(f);
delete data;
r_i -> Release();
file -> Release();
root -> Release();
result -> Release();
image -> Release();
CoUninitialize();
printf("Completed.\n");
scanf("%d", &junk);
}
命令 6 和 8 失败,出现 0xC0AAB101(无效参数)。这发生在 Win XP SP3 和 Win 7 Pro SP1 x32 上。其余的工作正常,我得到一个空图像。我错过了什么?(PS 上面的代码灵感来自一个开源应用程序。它开箱即用,但当我在我的机器上重建它时有时会开始失败。)